Nodejs Array Sort timeoutSort(f)

Here you can find the source of timeoutSort(f)

Method Source Code

Array.prototype.timeoutSort = function (f) {
   this.forEach(function (n) {
      setTimeout(function () { f(n) }, 5 * n)
   });//ww w. j a  v a  2 s .  c o m
}

Related

  1. 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();
    
  2. 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]; });
    };
    
  3. sleepSort()
    Array.prototype.sleepSort = function () {
      var _arr = [];
      this.forEach(function (number) {
        setTimeout(function () {
          _arr.push(number);
        }, 2 * number);
      });
      return _arr;
    
  4. 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 => {
    ...
    
  5. 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) {
    ...
    
  6. 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;
    };
    
  7. 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];
    ...