how to sort mixed numeric/alphanumeric array in javascript

前端 未结 4 1431
栀梦
栀梦 2021-02-09 03:25

I have a mixed array that I need to sort by number, alphabet and then by digit-

[\'A1\', \'A10\', \'A11\', \'A12\', \'A3A\', \'A3B\', \'A3\', \'A4\', \'B10\', \'         


        
相关标签:
4条回答
  • 2021-02-09 03:44

    You could sort it with splitted array by type and check for equality first and then by type.

    var array = ['A1', 'A10', 'A11', 'A12', 'A3A', 'A3B', 'A3', 'A4', 'B10', 'B2', 'F1', '1', '2', 'F3'];
    
    array.sort(function (a, b) {
        var isNumber = function (v) { return (+v).toString() === v; },
            aa = a.match(/\d+|\D+/g),
            bb = b.match(/\d+|\D+/g),
            i = 0,
            l = Math.min(aa.length, bb.length);
    
        while (i < l && aa[i] === bb[i]) {
            i++;
        }
        if (i === l) {
            return aa.length - bb.length;
        }
        if (isNumber(aa[i]) && isNumber(bb[i])) {
            return aa[i] - bb[i];
        }
        return aa[i].localeCompare(bb[i]);
    });
    
    console.log(array);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    0 讨论(0)
  • 2021-02-09 03:51
    var a=[ {
        "LIST": "14:NATURAL RESOURCES"
    },
    {
        "LIST": "7:DIVERSITY IN LIVING ORGANISMS"
    },
    {
        "LIST": "3:ATOMS AND MOLECULES"
    },
    {
        "LIST": "10:GRAVITATION"
    },
    {
        "LIST": "6:TISSUES"
    }]
    
    var c=a.sort(function(a,b){
    let aa=a.LIST,bb=b.LIST;
    return parseInt(aa.split(":")[0] - bb.split(":")[0])
    
    })
    
    console.log(c)
    
    0 讨论(0)
  • 2021-02-09 03:52

    Try this functionality. it give the result which you want exactly

       var arr = ['A1', 'A10', 'A11', 'A12', 'A3A', 'A3B', 'A3', 'A4', 'B10', 'B2', 'F1', '1', '2', 'F3'];
    function sortFn(a, b) {     
        var ax = [], bx = [];
        a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]) });
        b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]) });        
        while(ax.length && bx.length) {
            var an = ax.shift();
            var bn = bx.shift();
            var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
            if(nn) return nn;
        }
        return ax.length - bx.length;      
    }
    console.log(arr.sort(sortFn));

    0 讨论(0)
  • 2021-02-09 04:03

    var arr = ['A1', 'A10', 'A11', 'A12', 'A3A', 'A3B', 'A3', 'A4', 'B10', 'B2', 'F1', '1', '2', 'F3'];
    
    // regular expression to get the alphabetic and the number parts, if any
    var regex = /^([a-z]*)(\d*)/i;
    
    function sortFn(a, b) {
      var _a = a.match(regex);
      var _b = b.match(regex);
    
      // if the alphabetic part of a is less than that of b => -1
      if (_a[1] < _b[1]) return -1;
      // if the alphabetic part of a is greater than that of b => 1
      if (_a[1] > _b[1]) return 1;
    
      // if the alphabetic parts are equal, check the number parts
      var _n = parseInt(_a[2]) - parseInt(_b[2]);
      if(_n == 0) // if the number parts are equal start a recursive test on the rest
          return sortFn(a.substr(_a[0].length), b.substr(_b[0].length));
      // else, just sort using the numbers parts
      return _n;
    }
    
    console.log(arr.sort(sortFn));

    Note: the i modifier in the regular expression (/.../i) means case-insensitive (looks for both lowercases and uppercases).

    0 讨论(0)
提交回复
热议问题