如何用数字方式对javascript中的字符串进行排序 [英] how to sort strings in javascript numerically

查看:105
本文介绍了如何用数字方式对javascript中的字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对字符串数组(在javascript中)进行排序,以便将字符串中的数字组作为整数而不是字符串进行比较。我并不担心签名或浮点数。

I would like to sort an array of strings (in javascript) such that groups of digits within the strings are compared as integers not strings. I am not worried about signed or floating point numbers.

例如,结果应该是 [a1b3,a9b2,a10b2 ,a10b11] 不是 [a1b3,a10b11,a10b2,a9b2]

for example, the result should be ["a1b3","a9b2","a10b2","a10b11"] not ["a1b3","a10b11","a10b2","a9b2"]

最简单的方法似乎是在数字组周围的边界上拆分每个字符串。是否有一个模式,我可以传递给String.split分割字符边界而不删除任何字符?

The easiest way to do this seems to be splitting each string on boundaries around groups of digits. Is there a pattern I can pass to String.split to split on character boundaries without removing any characters?

abc11def22ghi.split(/? /)= [abc,11,def,22,ghi];

或者有没有另一种比较不涉及拆分的字符串的方法,可能是通过用前导零填充所有数字组,使它们的长度相同?

Or is there another way to compare strings that does not involve splitting them up, perhaps by padding all groups of digits with leading zeros so they are the same length?

aa1bb=> aa00000001bb,aa10bb=> aa00000010bb

我正在使用任意字符串,而不是具有特定数字组排列的字符串。

I am working with arbitrary strings, not strings that have a specific arrangement of digit groups.

编辑:

我喜欢 /(\d +)/ 一个班轮从盖比分裂数组。如何向后兼容?

I like the /(\d+)/ one liner from Gaby to split the array. How backwards compatible is that?

以可用于重建原始文件的方式解析字符串一次的解决方案比这个比较函数更有效。没有任何答案处理一些以数字开头的字符串而其他字符串不处理,但这很容易补救,并且在原始问题中没有明确说明。

The solutions that parse the strings once in a way that can be used to rebuild the originals are much more efficient that this compare function. None of the answers handle some strings starting with digits and others not, but that would be easy enough to remedy and was not explicit in the original question.

["a100","a20","a3","a3b","a3b100","a3b20","a3b3","!!","~~","9","10","9.5"].sort( function ( inA , inB ) {
    var                     result = 0;

    var                     a , b , pattern = /(\d+)/;
    var                     as = inA.split( pattern );
    var                     bs = inB.split( pattern );
    var                     index , count = as.length;

    if ( ( '' === as[0] ) === ( '' === bs[0] ) ) {
        if ( count > bs.length ) count = bs.length;

        for ( index = 0 ; index < count && 0 === result ; ++index ) {
            a = as[index]; b = bs[index];

            if ( index & 1 ) {
                result = a - b;
            } else {
                result = !( a < b ) ? ( a > b ) ? 1 : 0 : -1;
            }
        }

        if ( 0 === result ) result = as.length - bs.length;
    } else {
        result = !( inA < inB ) ? ( inA > inB ) ? 1 : 0 : -1;
    }

    return result;
} ).toString();

结果:!!,9,9.5,10,a3,a3b ,a3b3,a3b20,a3b100,a20,a100,~~

推荐答案

我认为这样做你想要什么

I think this does what you want

function sortArray(arr) {
    var tempArr = [], n;
    for (var i in arr) {
        tempArr[i] = arr[i].match(/([^0-9]+)|([0-9]+)/g);
        for (var j in tempArr[i]) {
            if( ! isNaN(n = parseInt(tempArr[i][j])) ){
                tempArr[i][j] = n;
            }
        }
    }
    tempArr.sort(function (x, y) {
        for (var i in x) {
            if (y.length < i || x[i] < y[i]) {
                return -1; // x is longer
            }
            if (x[i] > y[i]) {
                return 1;
            }
        }
        return 0;
    });
    for (var i in tempArr) {
        arr[i] = tempArr[i].join('');
    }
    return arr;
}
alert(
    sortArray(["a1b3", "a10b11", "a10b2", "a9b2"]).join(",")
);

这篇关于如何用数字方式对javascript中的字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆