Nodejs Array Sort sleepSort()

Here you can find the source of sleepSort()

Method Source Code

/**/*from  ww w.  j av a 2 s . co m*/
 * @example
 * 
 * var arr = [6,4,2,8,1,2].sleepSort();
 * console.log(arr); // [1,2,2,4,6,8]
 * 
 */
Array.prototype.sleepSort = function () {
  var _arr = [];
  this.forEach(function (number) {
    setTimeout(function () {
      _arr.push(number);
    }, 2 * number);
  });
  return _arr;
}

Related

  1. natsort(direction)
    Array.prototype.natsort = function(direction){
      direction = (direction ==-1) ? -1 : 1;
      if(direction == -1){
        this.sort(function(a,b){return (b-a);});
      } else {
        this.sort(function(a,b){return (a-b);});
      return this;
    };
    ...
    
  2. 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)){
    ...
    
  3. numberSort()
    Array.prototype.numberSort=function(){
      this.sort(function(a,b){
        return a-b;
      });
    
  4. 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();
    
  5. 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]; });
    };
    
  6. 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 => {
    ...
    
  7. 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) {
    ...
    
  8. timeoutSort(f)
    Array.prototype.timeoutSort = function (f) {
      this.forEach(function (n) {
        setTimeout(function () { f(n) }, 5 * n)
      });
    
  9. 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;
    };