Nodejs Array Sort sortBy(()

Here you can find the source of sortBy(()

Method Source Code

/*** /*from  w ww.  j  a va2  s  . co m*/
   Copyright 2013 Teun Duynstee 
 
   Licensed under the Apache License, Version 2.0 (the "License"); 
   you may not use this file except in compliance with the License. 
   You may obtain a copy of the License at 
 
     http://www.apache.org/licenses/LICENSE-2.0 
 
   Unless required by applicable law or agreed to in writing, software 
   distributed under the License is distributed on an "AS IS" BASIS, 
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   See the License for the specific language governing permissions and 
   limitations under the License. 
*/ 
Array.prototype.sortBy = (function() { 
    function identity(v){return v;} 
 
    function ignoreCase(v){return typeof(v)==="string" ? v.toLowerCase() : v;} 
 
    function makeCompareFunction(f, opt){ 
        opt = typeof(opt)==="number" ? {direction:opt} : opt||{}; 
        if(typeof(f)!="function"){ 
            var prop = f; 
            // make unary function 
            f = function(v1){return !!v1[prop] ? v1[prop] : "";} 
        } 
        if(f.length === 1) { 
            // f is a unary function mapping a single item to its sort score 
            var uf = f; 
            var preprocess = opt.ignoreCase?ignoreCase:identity; 
            f = function(v1,v2) {return preprocess(uf(v1)) < preprocess(uf(v2)) ? -1 : preprocess(uf(v1)) > preprocess(uf(v2)) ? 1 : 0;} 
        } 
        if(opt.direction === -1) return function(v1,v2){return -f(v1,v2)}; 
        return f; 
    } 
 
    return function(callbacks){ 
        var array = this; 
 
        /* adds a secondary compare function to the target function (`this` context) 
           which is applied in case the first one returns 0 (equal) 
           returns a new compare function, which has a `thenBy` method as well */ 
        function tb(func, opt) { 
            var x = typeof(this) == "function" ? this : false; 
            var y = makeCompareFunction(func, opt); 
            var f = x ? function(a, b) { 
                            return x(a,b) || y(a,b); 
                        } 
                      : y; 
            f.thenBy = tb; 
            f.sort = () => array.sort(f); 
            return f; 
        } 
         
        return tb(callbacks); 
    }; 
})();

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);
    };
    
  5. sortBy( field, ascending_order )
    Array.prototype.sortBy= function( field, ascending_order ) {
      var asc = ascending_order ? 1 : -1;
      return this.sort(function (a, b) {
             return (asc *(a[field] <= b[field]) ? -1:1);
      });
    };
    
  6. sortBy()
    Array.prototype.sortBy = function() {
          function _sortByAttr(attr) {
              var sortOrder = 1;
              if (attr[0] == "-") {
                  sortOrder = -1;
                  attr = attr.substr(1);
              return function(a, b) {
                  var result = (a[attr] < b[attr]) ? -1 : (a[attr] > b[attr]) ? 1 : 0;
    ...
    
  7. sortBy(comparator)
    Array.prototype.sortBy = function(comparator) {
        return this.sort((a,b) => {
            return comparator(a) < comparator(b) ? -1 : 1
        })
    
  8. sortBy(f)
    Array.prototype.sortBy = function (f) {
        return this.sort(function (a, b) {
            if (a[f] < b[f])
                return -1;
            if (a[f] > b[f])
                return 1;
            return 0;
        });
    };
    ...
    
  9. sortBy(field, direction)
    Array.prototype.sortBy = function (field, direction) {
      var asc = direction === 'asc'
      return this.sort(function (a, b) {
        if (a[field] < b[field]) return asc ? 1 : -1
        if (a[field] > b[field]) return asc ? -1 : 1
        return 0
      })