排序数组元素(带数字的字符串),自然排序

Roo*_*eyl 49 javascript sorting jquery

我有一个像阵列;

["IL0 Foo", "PI0 Bar", "IL10 Baz", "IL3 Bob says hello"]
Run Code Online (Sandbox Code Playgroud)

并且需要对它进行排序以使它看起来像;

["IL0 Foo", "IL3 Bob says hello", "IL10 Baz", "PI0 Bar"]
Run Code Online (Sandbox Code Playgroud)

我尝试过一种排序功能;

function compare(a,b) {
  if (a < b)
     return -1;
  if (a > b)
    return 1;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是这给了订单

["IL0 Foo", "IL10 Baz", "IL3 Bob says hello", "PI0 Bar"]
Run Code Online (Sandbox Code Playgroud)

我试图想到一个可行的正则表达式,但无法理解它.
如果它有助于格式将始终是2个字母,x数量的数字,然后任意数量的字符.

geo*_*org 94

这称为"自然排序",可以在JS中实现,如下所示:

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;
}

/////////////////////////

test = [
    "img12.png",
    "img10.png",
    "img2.png",
    "img1.png",
    "img101.png",
    "img101a.png",
    "abc10.jpg",
    "abc10",
    "abc2.jpg",
    "20.jpg",
    "20",
    "abc",
    "abc2",
    ""
];

test.sort(naturalCompare)
document.write("<pre>" + JSON.stringify(test,0,3));
Run Code Online (Sandbox Code Playgroud)

要按相反顺序排序,只需交换参数:

test.sort(function(a, b) { return naturalCompare(b, a) })
Run Code Online (Sandbox Code Playgroud)

或者干脆

test = test.sort(naturalCompare).reverse();
Run Code Online (Sandbox Code Playgroud)

  • 任何人都可以帮助如何实现这个数组的升序和降序排序? (2认同)

Nin*_*olz 10

你可以使用String#localeCompareoptions

灵敏度

字符串中的哪些差异应导致非零结果值.可能的值是:

  • "base":只有基本字母不同的字符串才会比较为不相等.例如:a ? b,a = á,a = A.
  • "accent":只有基本字母或重音和其他变音符号不同的字符串才会比较为不相等.例如:a ? b,a ? á,a = A.
  • "case":只有基本字母或大小写不同的字符串才会比较为不相等.例如:a ? b,a = á,a ? A.
  • "variant":基本字母,重音符号和其他变音符号不同的字符串,或不相等的情况比较.其他差异也可以考虑在内.例如:a ? b,a ? á,a ? A.

使用"sort"的默认值为"variant"; 它的语言环境依赖于使用"搜索".

数字

是否应使用数字校对,使"1"<"2"<"10".可能的值是truefalse; 默认是false.可以通过options属性或通过Unicode扩展键设置此选项; 如果两者都提供,则options属性优先.实现不需要支持此属性.

var array = ["IL0 Foo", "PI0 Bar", "IL10 Baz", "IL3 Bob says hello"];

array.sort(function (a,b) {
    return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});

console.log(array);
Run Code Online (Sandbox Code Playgroud)


epa*_*llo 5

var re = /([a-z]+)(\d+)(.+)/i;
var arr = ["IL0 Foo", "PI0 Bar", "IL10 Baz", "IL3 Bob says hello"];
var order = arr.sort( function(a,b){
    var ma = a.match(re),
        mb = b.match(re),
        a_str = ma[1],
        b_str = mb[1],
        a_num = parseInt(ma[2],10),
        b_num = parseInt(mb[2],10),
        a_rem = ma[3],
        b_rem = mb[3];
    return a_str > b_str ? 1 : a_str < b_str ? -1 : a_num > b_num ? 1 : a_num < b_num ? -1 : a_rem > b_rem;  
});
Run Code Online (Sandbox Code Playgroud)