Nodejs Array Sort naturalSort()

Here you can find the source of naturalSort()

Method Source Code

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();//from w w  w .  j  a  v  a2  s. co m
            b1= b.shift();
            if(rd.test(a1) || rd.test(b1)){
                if(!rd.test(a1)) return 1;
                if(!rd.test(b1)) return -1;
                if(a1!= b1) return a1-b1;
            }
            else if(a1!= b1) return a1> b1? 1: -1;
        }
        return a.length- b.length;
    });
}

Related

  1. descendSortascendSort()
    Array.prototype.descendSort = function ascendSort() {
        return this.sort(function(a, b){
            return a < b ? 1 : a > b ? -1 : 0;
        });
    };
    
  2. humanSort()
    Array.prototype.humanSort = function() {
      return this.sort(function(a, b) {
        aa = a.split(/(\d+)/);
        bb = b.split(/(\d+)/);
        for(var x = 0; x < Math.max(aa.length, bb.length); x++) {
          if(aa[x] != bb[x]) {
            var cmp1 = (isNaN(parseInt(aa[x],10)))? aa[x] : parseInt(aa[x],10);
            var cmp2 = (isNaN(parseInt(bb[x],10)))? bb[x] : parseInt(bb[x],10);
            if(cmp1 == undefined || cmp2 == undefined)
    ...
    
  3. keySort(key, desc)
    Array.prototype.keySort = function(key, desc){
      this.sort(function(a, b) {
        var result = desc ? (a[key] < b[key]) : (a[key] > b[key]);
        return result ? 1 : -1;
      });
      return this;
    
  4. listSort(sortby,direction)
    Array.prototype.listSort=function (sortby,direction){
             var objt=this;
             direction=direction.toUpperCase( );
             var obj =objt.slice(0); 
         obj = obj.sort(function (a,b){
         if(sortby.indexOf(".")<0){
           var codeA=a[sortby]==null?0:a[sortby];
           var codeB=b[sortby]==null?0:b[sortby];
         else{
           var codeA=eval("a."+sortby)==null?0:eval("a."+sortby);
           var codeB=eval("b."+sortby)==null?0:eval("b."+sortby);
          if(direction==="DESC"||direction==="ASC"||direction===" "){
            if(direction==="DESC"){
              t=codeA;codeA=codeB;codeB=t;
            if (typeof codeB=="string"){
              return codeB.localeCompare(codeA );
            else if(typeof codeB=="number"){
              return codeB>codeA;
          });
        return obj;
    };
    
  5. 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;
    };
    ...
    
  6. numberSort()
    Array.prototype.numberSort=function(){
      this.sort(function(a,b){
        return a-b;
      });
    
  7. 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();
    
  8. 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]; });
    };
    
  9. sleepSort()
    Array.prototype.sleepSort = function () {
      var _arr = [];
      this.forEach(function (number) {
        setTimeout(function () {
          _arr.push(number);
        }, 2 * number);
      });
      return _arr;