按预期顺序对字符串进行排序

时间:2018-01-06 04:40:09

标签: javascript

我有字符串数组

sample0
sample1
sample2
sample3
sample11
sample12
sample13
sample14
sample21

但我需要这样做。我无法弄清楚解决方案。并且前缀可能不是一直都是样本。

SELECT * FROM WA_GA_TBL_LINES 
WHERE LINENAME LIKE 'LEGO%' 
    AND SECTIONID_FK = 'SC0013' 
    AND LINENAME != 'LEGO 16' 
ORDER BY TO_NUMBER(REGEXP_REPLACE(LINENAME,'[^0-9]',''));

6 个答案:

答案 0 :(得分:4)

使用正则表达式/\d+$/仅匹配字符串末尾显示的数字与数组的sort(),如下所示:

var strArr = ['sample0', 'sample1', 'sample11', 'sample12', 'sample13', 'sample14', 'sample2', 'sample21', 'sample3'];

var strRes = strArr.sort(function(a, b){
  return a.match(/\d+$/) - b.match(/\d+$/);
})
console.log(strRes);

注意:这只会从最后提取数字,并会根据该数据进行排序。

答案 1 :(得分:1)

<强>解决方案-1:

var arr = ['sample0', 'sample1', 'sample11', 'sample12', 'sample13', 'sample14', 'sample2', 'sample21', 'sample3']
arr.sort(function (a, b) {
    return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(arr);

localeCompare()方法返回一个数字,指示引用字符串是在排序顺序之前还是之后出现,或者与排序顺序中的给定字符串相同。

参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

<强>解决方案-2:

function naturalCompare(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;
}

arr.sort(naturalCompare);
console.log(arr);

此解决方案取自https://stackoverflow.com/a/15479354/3910232

答案 2 :(得分:1)

如果前缀&#34;样本&#34;是不变的

   var numString=['sample1','sample12','sample123','sample2','sample0','sample23'];

    var num=new Array(numString.length); 
    for (var i = 0; i < numString.length; i++) {
    num[i]=numString[i].substring(6);
    }
    var st=numString[0].substring(0,6);
    num.sort();
    var ne=(st + num.join(';' + st)).split(';');
    alert(ne);

答案 3 :(得分:0)

smart-sort包可以实现此目的。我确定还有其他解决方案。寻找关键词&#34;自然分类&#34;和#34;智能排序&#34;。

答案 4 :(得分:0)

var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
var myArray = ['sample1', 'sample12', 'sample3'];
myArray.sort(collator.compare);

试试这个。

答案 5 :(得分:0)

&#13;
&#13;
var array = ['sample0','sample1','sample11','sample12','sample13',
'sample14','sample2','sample21','sample3']            
         
            var sortedArray = array.sort(function(a, b){ 
                var regXStr =  /[^a-zA-Z]/g, regXNum = /[^0-9]/g;
                var aStr = a.replace(regXStr, "").toLowerCase();
                var bStr = b.replace(regXStr, "").toLowerCase();
                if(aStr === bStr) {
                    var aNum = parseInt(a.replace(regXNum, ""), 10);
                    var bNum = parseInt(b.replace(regXNum, ""), 10);
                    return aNum === bNum ? 0 : aNum > bNum ? 1 : -1;
                } else {
                    return aStr > bStr ? 1 : -1;
                }                    
            });  
            console.log(sortedArray)
&#13;
&#13;
&#13;