Nodejs Array Sort sort(fn)

Here you can find the source of sort(fn)

Method Source Code

Array.prototype.sort = function(fn) {
   if (!arguments[0]) {
      fn = function(a, b) {
         return a.compareTo(b);
      };//from   ww w.j  a  v a2  s .  com
   }
   
   for (var i = 0, len = this.length; i < len - 1; i++) {
      for (var j = i + 1, len = this.length; j < len; j++) {
         if (fn(this[i], this[j]) > 0) {
            var tmp = this[i];
            this[i] = this[j];
            this[j] = tmp;
         }
      }
   }
   
   return this;
};

Related

  1. sortABC( property )
    Array.prototype.sortABC = function( property )
      return this.sort( function( a,b)
        var links = a[property].replace(/?, "Oe").replace(/?, "Ae").replace(/?,"Ue");
        var rechts = b[property].replace(/?, "Oe").replace(/?, "Ae").replace(/?,"Ue");
        if( links < rechts) return -1;
        if( links > rechts) return 1;
        return 0;
    ...
    
  2. sortAscending()
    Array.prototype.sortAscending = function() {
      return this.sort( function( a, b ) {
        if ( a > b ) {
          return 1;
        return a < b ? -1 : 0;
      } );
    };
    
  3. sortAscending()
    Array.prototype.sortAscending = function() {
      this.sort(function(a, b) {
        return a - b;
      });
      return this;
    };
    var largestProductOfThree = function(array) {
      array = array.slice().sortAscending();
      var secondFromLast = array[array.length - 2];
    ...
    
  4. sortBool(iteratee)
    Array.prototype.sortBool = function(iteratee) {
        var _iteratee = function(a, b) {return iteratee(a, b) ? -1 : 1};
        return this.sort(_iteratee);
    };