2

好的,我有一段代码按字母顺序对给出的名称进行排序。

然而,代码并没有按照我想要的方式处理小数。

它以以下方式对名称进行排序(我希望它以数字方式递增):

它会订购它:

  • 苹果 - 1.0051
  • 苹果 - 1.1071
  • 苹果 - 11.1592
  • 苹果 - 12.0692
  • 苹果 - 12.1717
  • APPLE - 2.0186 << 这显然应该在“APPLE - 1.1071”之后
  • 苹果 - 21.1407
  • 苹果 - 22.089
  • 苹果 - 23.069
  • 香蕉 - 1.0051
  • 香蕉 - 1.1071
  • 香蕉 - 11.1592
  • 香蕉 - 12.0692
  • 香蕉 - 12.1717
  • BANANA - 2.0186 << 这显然应该在“BANANA - 1.1071”之后
  • 香蕉 - 21.1407
  • 香蕉 - 22.089
  • 香蕉 - 23.069

这是我正在使用的代码。我不完全理解代码,因为它是我一直在使用的一个片段。

function(a, b){
         var nameA=a.myname.toLowerCase(), nameB=b.myname.toLowerCase()
         if (nameA < nameB) //sort string ascending
          return -1 
         if (nameA > nameB)
          return 1
         return 0 //default return value (no sorting)
    }

问候,强尼

4

5 回答 5

3

Demo ,这里是源码+小解释:

function fruit_sort(a, b) {
    var parts = {
        a: a.split('-'),
        b: b.split('-')
    };
    if (parts.a[0] == parts.b[0]) // strings are the same
        return parseFloat(parts.a[1]) - parseFloat(parts.b[1]); // sort by number
    return parts.a[0] > parts.b[0] ? 1 : -1; // sort by string
}
var arr = ["APPLE - 1.0051",
    "APPLE - 1.1071",
    "APPLE - 11.1592",
    "APPLE - 12.0692",
    "BANANA - 1.0051",
    "BANANA - 1.1071",
    "BANANA - 11.1592",
    "BANANA - 12.0692",
    "BANANA - 12.1717",
    "APPLE - 12.1717",
    "APPLE - 2.0186",
    "APPLE - 21.1407",
    "BANANA - 23.069",
    "APPLE - 22.089",
    "APPLE - 23.069",
    "BANANA - 2.0186",
    "BANANA - 21.1407",
    "BANANA - 22.089"];
arr.sort(fruit_sort);
console.log(arr);
// outputs
[
    "APPLE - 1.0051",
    "APPLE - 1.1071",
    "APPLE - 2.0186",
    "APPLE - 11.1592",
    "APPLE - 12.0692",
    "APPLE - 12.1717",
    "APPLE - 21.1407",
    "APPLE - 22.089",
    "APPLE - 23.069",
    "BANANA - 1.0051",
    "BANANA - 1.1071",
    "BANANA - 2.0186",
    "BANANA - 11.1592",
    "BANANA - 12.0692",
    "BANANA - 12.1717",
    "BANANA - 21.1407",
    "BANANA - 22.089",
    "BANANA - 23.069"
]

首先,该函数将术语拆分为它们的文本和数字部分——如果文本是偶数,它只按数值的 parseFloat 值排序——否则它首先按字符串值排序。

于 2013-01-26T20:23:30.007 回答
2

Like @Steve Wellens suggested in his comment, I modified your existing snippet to compare the text parts first, then in the event of a tie compare the numeric portions of the string, by converting them to floats. If you would like to sort solely by the numeric portion of the string then @extramaster 's answer should serve you well.

function(a, b){
        var parts, textPartA, textPartB, numericPartA, numericPartB;

        parts = a.split('-');
        textPartA = parts[0];
        numericPartA = parseFloat(parts[1]);

        parts = b.split('-');
        textPartB = parts[0];
        numericPartB = parseFloat(parts[1]);

        //sort by text first
        if(textPartA < textPartB) {
            return -1;
        }
        if(textPartA > textPartB) {
            return 1;
        }

        //text parts are equal, now sort by the numeric parts
        if(numericPartA < numericPartB){
            return -1;
        }
        if(numericPartA > numericPartB){
            return 1;
        }

        return 0;
    }

@Jonny: A quick example

于 2013-01-26T19:34:17.927 回答
2

试试这个代码:

function (x, y) {
    x=x.myname;
    y=y.myname;

    var nameA = x.toLowerCase().split("-")[0],
        nameB = y.toLowerCase().split("-")[0]
    if (nameA < nameB) //sort string ascending
    return -1
    if (nameA > nameB) return 1
    var floatExtract = /(([1-9][0-9]*\.?[0-9]*)|(\.[0-9]+))([Ee][+-]?[0-9]+)?/;
    if (floatExtract.test(x) && floatExtract.test(y)) {
        x = x.match(floatExtract)[1];
        y = y.match(floatExtract)[1];
        if (!isNaN(parseFloat(x)) && !isNaN(parseFloat(y))) {
            x = parseInt(x);
            y = parseInt(y);
        }
        return ((x > y) ? 1 : ((x < y) ? -1 : 0));
    }
    return 0;
}

它拆分您的字符串并与第一部分[连字符之前]进行简单比较,然后在您的字符串中搜索一个浮点数并在列表中针对该浮点数进行另一种排序......

演示| 资源

于 2013-01-26T19:12:25.633 回答
1

这是另一个页面上的类似解决方案: Javascript notsorting DECIMAL numbers correct

这个想法是先按字符串排序,然后按数字排序。

于 2016-11-30T09:53:19.947 回答
0

好的,我使用了 rlemon 的代码示例并将其编辑为以下内容:

function(a, b) {
var parts = {
    a: a.myname.split(' - '),
    b: b.myname.split(' - ')
};
if (parts.a[0] == parts.b[0]) // strings are the same
    return parseFloat(parts.a[1]) - parseFloat(parts.b[1]); // sort by number
return parts.a[0] > parts.b[0] ? 1 : -1; // sort by string

}

于 2013-01-26T20:47:08.070 回答