Nodejs Array Sort stableSort(f)

Here you can find the source of stableSort(f)

Method Source Code

Array.prototype.stableSort = function(f) {
  return this.map((ele, i) => ({ele: ele, i: i}))
  .sort((a, b) => {/*from www  .  j  a va  2 s  .co m*/
    var val = f(a.ele, b.ele);
    if (val === 0) {
      return a.i - b.i;
    }
    return val;
  }).map(ele => {
    return ele.ele;
  });
};

Related

  1. naturalSort()
    Array.prototype.naturalSort= function(){
        var a, b, a1, b1, rx=/(\d+)|(\D+)/g, rd=/\d+/;
        return this.sort(function(as, bs){
            a= String(as).toLowerCase().match(rx);
            b= String(bs).toLowerCase().match(rx);
            while(a.length && b.length){
                a1= a.shift();
                b1= b.shift();
                if(rd.test(a1) || rd.test(b1)){
    ...
    
  2. numberSort()
    Array.prototype.numberSort=function(){
      this.sort(function(a,b){
        return a-b;
      });
    
  3. 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();
    
  4. 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]; });
    };
    
  5. sleepSort()
    Array.prototype.sleepSort = function () {
      var _arr = [];
      this.forEach(function (number) {
        setTimeout(function () {
          _arr.push(number);
        }, 2 * number);
      });
      return _arr;
    
  6. superSort()
    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) {
    ...
    
  7. timeoutSort(f)
    Array.prototype.timeoutSort = function (f) {
      this.forEach(function (n) {
        setTimeout(function () { f(n) }, 5 * n)
      });
    
  8. 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;
    };
    
  9. 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];
    ...