Javascript - 对字母数字组合进行排序

发布于 2024-11-07 17:10:27 字数 330 浏览 0 评论 0原文

我有字母和数字的组合。例如:2E12、1Z10、3D13、3D03、FB14、X002 等。

我尝试了一些方法来对这些字符串进行排序,但似乎没有任何效果。 parseInt 在块中工作,但整个数组从未排序(它是一个 json 数组),如果第二次运行排序,则会出现不同的结果。

我还尝试使用正则表达式将所有字母替换为数字,但这会产生逻辑错误。每次替换字符串中间的大字母时,数字都会增加 10 或 20 倍。例如,1Z10 将创建 12610,即使它以 1 开头并且应向顶部排序。

有谁知道如何对这些字符串进行排序?字母在前还是数字在前并不重要,只要我能摆脱随机的情况即可。

提前致谢!

I have a combination of letters and numbers. For example: 2E12, 1Z10, 3D13, 3D03, FB14, X002, etc.

I've tried a handful of methods to sort these strings, but nothing seems to work. parseInt works in clumps but the whole array is never sorted (it is a json array) and different results appear if the sort is run a second time.

I've also tried using regex to replace all of the letters with numbers, but this creates a logic error. Each time a large letter in the middle of the string is replaced it increases the number by a factor of 10 or 20. For example, 1Z10 would create 12610 even though it starts with a 1 and should sort towards the top.

Does anyone know of how to sort these strings? It doesn't matter if the letter is first or the number is first, as long as I can get away from the random smatterings.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2024-11-14 17:10:27

如果您希望数字序列像数字一样排序,则在字母和之前
这样 2 之后的 100 次排序,你需要所谓的自然排序 -

这是一个例子,谷歌会找到更多。

// case insensitive, digits to number interpolation

function natSort(as, bs){
    var a, b, a1, b1, i= 0, L, rx=  /(\d+)|(\D+)/g, rd=  /\d/;
    if(isFinite(as) && isFinite(bs)) return as - bs;
    a= String(as).toLowerCase();
    b= String(bs).toLowerCase();
    if(a=== b) return 0;
    if(!(rd.test(a) && rd.test(b))) return a> b? 1: -1;
    a= a.match(rx);
    b= b.match(rx);
    L= a.length> b.length? b.length: a.length;
    while(i < L){
        a1= a[i];
        b1= b[i++];
        if(a1!== b1){
            if(isFinite(a1) && isFinite(b1)){
                if(a1.charAt(0)=== "0") a1= "." + a1;
                if(b1.charAt(0)=== "0") b1= "." + b1;
                return a1 - b1;
            }
            else return a1> b1? 1: -1;
        }
    }
    return a.length - b.length;
}

var s= '2E12, 1Z10, 1z2, 3D13, 3D03, FB14, X002'.split(', ');

s.sort(natSort)

/*  returned value: (Array)
1z2,1Z10,2E12,3D03,3D13,FB14,X002
*/

if you want digit sequences to sort as if they were numbers, before alphas and
so that 100 sorts after 2, you need what is called a natural sort-

This is one example, Google will find more.

// case insensitive, digits to number interpolation

function natSort(as, bs){
    var a, b, a1, b1, i= 0, L, rx=  /(\d+)|(\D+)/g, rd=  /\d/;
    if(isFinite(as) && isFinite(bs)) return as - bs;
    a= String(as).toLowerCase();
    b= String(bs).toLowerCase();
    if(a=== b) return 0;
    if(!(rd.test(a) && rd.test(b))) return a> b? 1: -1;
    a= a.match(rx);
    b= b.match(rx);
    L= a.length> b.length? b.length: a.length;
    while(i < L){
        a1= a[i];
        b1= b[i++];
        if(a1!== b1){
            if(isFinite(a1) && isFinite(b1)){
                if(a1.charAt(0)=== "0") a1= "." + a1;
                if(b1.charAt(0)=== "0") b1= "." + b1;
                return a1 - b1;
            }
            else return a1> b1? 1: -1;
        }
    }
    return a.length - b.length;
}

var s= '2E12, 1Z10, 1z2, 3D13, 3D03, FB14, X002'.split(', ');

s.sort(natSort)

/*  returned value: (Array)
1z2,1Z10,2E12,3D03,3D13,FB14,X002
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文