Nodejs Array Sort sortAscending()

Here you can find the source of sortAscending()

Method Source Code

/* Write a function that finds the largest possible product of any three numbers
 * from an array./*www .  ja v  a 2 s  .c  o m*/
 *
 * Example:
 * largestProductOfThree([2, 1, 3, 7]) === 42
 *
 * Extra credit: Make your function handle negative numbers.
 */

// Create a convenience function that sorts arrays ascending numerically
Array.prototype.sortAscending = function() {
  this.sort(function(a, b) {
    return a - b;
  });
  return this;
};

var largestProductOfThree = function(array) {

  // Make a copy of the input array and sort it numerically
  array = array.slice().sortAscending();

  var secondFromLast = array[array.length - 2];
  var thirdFromLast = array[array.length - 3];

  return array[array.length - 1] * Math.max(secondFromLast * thirdFromLast, array[0] * array[1]);
};

Related

  1. sort(fn)
    Array.prototype.sort = function(fn) {
      if (!arguments[0]) {
        fn = function(a, b) {
          return a.compareTo(b);
        };
      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) {
    ...
    
  2. 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;
    ...
    
  3. sortAscending()
    Array.prototype.sortAscending = function() {
      return this.sort( function( a, b ) {
        if ( a > b ) {
          return 1;
        return a < b ? -1 : 0;
      } );
    };
    
  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 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; 
                f = function(v1){return !!v1[prop] ? v1[prop] : "";} 
            if(f.length === 1) { 
                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; 
            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); 
        }; 
    })();
    
  7. 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;
    ...