Nodejs Array Sort superSort()

Here you can find the source of superSort()

Method Source Code

/**/*from  ww  w  . jav  a  2 s  .c  o m*/
 * @author Benjamin Berman
 * ? 2014 All Rights Reserved
 **/
// Adapted from http://stackoverflow.com/a/11379791/1757994
Array.prototype.superSort = function() {
    function dynamicSort(property) {
        return function (obj1,obj2) {
            return obj1[property] > obj2[property] ? 1
                : obj1[property] < obj2[property] ? -1 : 0;
        }
    }

    var props = arguments;

    return this.sort(function (obj1, obj2) {
        var i = 0, result = 0, numberOfProperties = props.length;
        /* try getting a different result from 0 (equal)
         * as long as we have extra properties to compare
         */
        while(result === 0 && i < numberOfProperties) {
            result = dynamicSort(props[i])(obj1, obj2);
            i++;
        }
        return result;
    });
};

Related

  1. numberSort()
    Array.prototype.numberSort=function(){
      this.sort(function(a,b){
        return a-b;
      });
    
  2. pancake_sort()
    Array.prototype.pancake_sort = function () {
        for (var i = this.length - 1; i >= 1; i--) {
            var max_idx = 0;
            var max = this[0];
            for (var j = 1; j <= i; j++) {
                if (this[j] > max) {
                    max = this[j];
                    max_idx = j;
            if (max_idx == i)
                continue; 
            var new_slice;
            if (max_idx > 0) {
                new_slice = this.slice(0, max_idx+1).reverse();
                for (var j = 0; j <= max_idx; j++)
                    this[j] = new_slice[j];
            new_slice = this.slice(0, i+1).reverse();
            for (var j = 0; j <= i; j++)
                this[j] = new_slice[j];
        return this;
    ary = [7,6,5,9,8,4,3,1,2,0]
    sorted = ary.concat().pancake_sort();
    
  3. propSortpropSort(prop)
    Array.prototype.propSort = function propSort(prop) {
      return this.sort(function(a, b) { return +a[prop] - +b[prop]; });
    };
    Array.prototype.propAsort = function propSort(prop) {
      return this.sort(function(a, b) { return +b[prop] - +a[prop]; });
    };
    
  4. sleepSort()
    Array.prototype.sleepSort = function () {
      var _arr = [];
      this.forEach(function (number) {
        setTimeout(function () {
          _arr.push(number);
        }, 2 * number);
      });
      return _arr;
    
  5. stableSort(f)
    Array.prototype.stableSort = function(f) {
      return this.map((ele, i) => ({ele: ele, i: i}))
      .sort((a, b) => {
        var val = f(a.ele, b.ele);
        if (val === 0) {
          return a.i - b.i;
        return val;
      }).map(ele => {
    ...
    
  6. timeoutSort(f)
    Array.prototype.timeoutSort = function (f) {
      this.forEach(function (n) {
        setTimeout(function () { f(n) }, 5 * n)
      });
    
  7. validate_sorted()
    Array.prototype.validate_sorted = function(){
      for (var i = 0; i < this.length - 1; i++){
        if (this[i] > this[i+1])
          return false;
      return true;
    };
    
  8. Array.prototype.qsort(left, right)
    Array.prototype.qsort = function (left, right) {
      var pivot, newPivot, pivotVal, tmp;
      if (left === undefined) {
        left = 0;
        right = this.length - 1;
      if (left < right) {
        pivot = left + Math.ceil((right - left) / 2);
        pivotVal = this[pivot];
    ...