Nodejs Array Sort alphanumSort(caseInsensitive)

Here you can find the source of alphanumSort(caseInsensitive)

Method Source Code

Array.prototype.alphanumSort = function(caseInsensitive) {
  for (var z = 0, t; t = this[z]; z++) {
    this[z] = new Array();/*from  w  w  w .  j a v a  2s.  co  m*/
    var x = 0, y = -1, n = 0, i, j;

    while (i = (j = t.charAt(x++)).charCodeAt(0)) {
      var m = (i == 46 || (i >=48 && i <= 57));
      if (m !== n) {
        this[z][++y] = "";
        n = m;
      }
      this[z][y] += j;
    }
  }

  this.sort(function(a, b) {
    for (var x = 0, aa, bb; (aa = a[x]) && (bb = b[x]); x++) {
      if (caseInsensitive) {
        aa = aa.toLowerCase();
        bb = bb.toLowerCase();
      }
      if (aa !== bb) {
        var c = Number(aa), d = Number(bb);
        if (c == aa && d == bb) {
          return c - d;
        } else return (aa > bb) ? 1 : -1;
      }
    }
    return a.length - b.length;
  });

  for (var z = 0; z < this.length; z++)
    this[z] = this[z].join("");
}

var arrayToSort = ["20", "wammer", "garden cart", "14", "hammer"];

console.log('before sort: ' + arrayToSort);


arrayToSort.alphanumSort();

console.log('after sort: ' + arrayToSort);

Related

  1. sortObject(field)
    Array.prototype.sortObject = function(field) {
      if (typeof this[0][field] === 'undefined') return this;
      var len = this.length,
        i,
        j,
        flag,
        next,
        temp;
      for (i = 1; i < len; i++) {
    ...
    
  2. sort_text(direction, index)
    Array.prototype.sort_text = function(direction, index) {
        direction = direction === undefined ? 'asc' : direction;
        direction = direction === 'asc' ? 1 : -1;
        this.sort(function(a, b) {
            if (a[index] < b[index]) return -direction;
            if (a[index] > b[index]) return direction;
            return 0;
        })
    };
    ...
    
  3. sort()
    Array.prototype.sort = function(){
      var a = this;
      for(var i = 0;i<a.length-1;i++){
        var t = i;
        for(var j = i+1;j<a.length;j++)
          if(a[t]>a[j])
            t = j;
        if(t!=i)
          this.swap(i,t);
    ...
    
  4. alphanumSort( caseInsensitive )
    Array.prototype.alphanumSort = function( caseInsensitive ) {
      for ( var z = 0, t; t = this[z]; z++ ) {
        this[z] = [];
        var x = 0, y = -1, n = 0, i, j;
        while (i = ( j = t.charAt( x++ ) ).charCodeAt( 0 ) ) {
          var m = ( i == 46 || ( i >=48 && i <= 57 ) );
          if ( m !== n ) {
            this[z][++y] = "";
            n = m;
    ...
    
  5. alphanumSort(caseInsensitive)
    Array.prototype.alphanumSort = function(caseInsensitive) {
      for (var z = 0, t; t = this[z]; z++) {
        this[z] = [];
        var x = 0, y = -1, n = 0, i, j;
        while (i = (j = t.charAt(x++)).charCodeAt(0)) {
          var m = (i == 46 || (i >=48 && i <= 57));
          if (m !== n) {
            this[z][++y] = "";
            n = m;
    ...
    
  6. ascendSortascendSort()
    'use strict'
    Array.prototype.ascendSort = function ascendSort() {
        return this.sort(function(a, b){
            return a > b ? 1 : a < b ? -1 : 0;
        });
    };
    
  7. asort(key)
    function xsort(sqs,col,order){
        sqs.sort(function(a,b){
            return((a[col]-b[col])*order);
        });
        return(sqs);
    Array.prototype.asort = function(key) {
        this.sort(function(a, b) {
            return (a[key] > b[key]) ? 1 : -1;
    ...
    
  8. countingSort()
    Array.prototype.countingSort = function() {
      var maxDigit = this[0];
      for(var i = 1; i < this.length; i++) {
        if(this[i] > maxDigit) {
          maxDigit = this[i];
      var bucket = new Array(maxDigit + 1);
      for(var i = 0; i < this.length; i++) {
    ...
    
  9. descendSortascendSort()
    Array.prototype.descendSort = function ascendSort() {
        return this.sort(function(a, b){
            return a < b ? 1 : a > b ? -1 : 0;
        });
    };