为什么javascript不按数字顺序排序数字数组?

时间:2022-11-29 15:59:45

I have faced with strange sorting result in Javascript for numeric arrays. For example result of sorting [1,2,10,20,100,200]:

我在Javascript中面对数字数组的奇怪排序结果。例如排序[1,2,10,20,100,200]的结果:

> [1, 2, 10, 20, 100, 200].sort()
[ 1, 10, 100, 2, 20, 200 ]

Why it's happening?

为什么会这样?

It seems what the array sort cannot be used for sorting numeric arrays directly?

似乎数组排序不能直接用于排序数字数组?

5 个解决方案

#1


8  

From the MDN documentation:

从MDN文档:

If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order.

如果未提供compareFunction,则通过将元素转换为字符串并按字典(“词典”或“电话簿”,而不是数字)顺序比较字符串来对元素进行排序。

(or see the relevant part of the EMCAScript specification, hat tip to Felix Kling for digging up the reference)

(或者参见EMCAScript规范的相关部分,向Felix Kling提示挖掘参考)

If you want to do a numeric sort, then pass a compare function:

如果要进行数字排序,则传递比较函数:

[1, 2, 10, 20, 100, 200].sort(function (a,b) { return a-b; });

#2


1  

Sorting arrays in JavaScript is done via the method array.sort(). Calling sort() by itself simply sorts the array in alphabetical order, for example:

JavaScript中的数组排序是通过array.sort()方法完成的。调用sort()本身只是按字母顺序对数组进行排序,例如:

var array=[40, 6, 300]; 
array.sort();

Array now becomes [300,40,6], the function is going to sort only by first number.

数组现在变为[300,40,6],该函数仅按第一个数字排序。

In this case to sort write a function:

在这种情况下要排序写一个函数:

Array.sort(function(number1, number2){
    return number1-number2;  
}) 

Array now becomes [6,40,300].

数组现在变为[6,40,300]。

#3


0  

array.sort only sorts strings. To sort numbers use this:

array.sort只对字符串进行排序。要对数字进行排序,请使

[1, 2, 10, 20, 100, 200].sort(function(a,b) { return a-b });

#4


0  

Try this for numeric sorting:

试试这个数字排序:

[ 1, 10, 100, 2, 20, 200 ].sort(function(a,b){return a-b})

#5


0  

The sort() method calls the String() casting function on every item and then compares the strings to determine the correct order. This occurs even if all items in an array are numbers. Try this way:

sort()方法在每个项目上调用String()强制转换函数,然后比较字符串以确定正确的顺序。即使数组中的所有项都是数字,也会发生这种情况。试试这种方式:

function compare(value1, value2) { 
    if (value1 < value2) {
        return -1;
    } else if (value1 > value2) {
        return 1;
    } else {
    return 0; 
    }
}
[ 1, 10, 100, 2, 20, 200 ].sort(compare);

#1


8  

From the MDN documentation:

从MDN文档:

If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order.

如果未提供compareFunction,则通过将元素转换为字符串并按字典(“词典”或“电话簿”,而不是数字)顺序比较字符串来对元素进行排序。

(or see the relevant part of the EMCAScript specification, hat tip to Felix Kling for digging up the reference)

(或者参见EMCAScript规范的相关部分,向Felix Kling提示挖掘参考)

If you want to do a numeric sort, then pass a compare function:

如果要进行数字排序,则传递比较函数:

[1, 2, 10, 20, 100, 200].sort(function (a,b) { return a-b; });

#2


1  

Sorting arrays in JavaScript is done via the method array.sort(). Calling sort() by itself simply sorts the array in alphabetical order, for example:

JavaScript中的数组排序是通过array.sort()方法完成的。调用sort()本身只是按字母顺序对数组进行排序,例如:

var array=[40, 6, 300]; 
array.sort();

Array now becomes [300,40,6], the function is going to sort only by first number.

数组现在变为[300,40,6],该函数仅按第一个数字排序。

In this case to sort write a function:

在这种情况下要排序写一个函数:

Array.sort(function(number1, number2){
    return number1-number2;  
}) 

Array now becomes [6,40,300].

数组现在变为[6,40,300]。

#3


0  

array.sort only sorts strings. To sort numbers use this:

array.sort只对字符串进行排序。要对数字进行排序,请使

[1, 2, 10, 20, 100, 200].sort(function(a,b) { return a-b });

#4


0  

Try this for numeric sorting:

试试这个数字排序:

[ 1, 10, 100, 2, 20, 200 ].sort(function(a,b){return a-b})

#5


0  

The sort() method calls the String() casting function on every item and then compares the strings to determine the correct order. This occurs even if all items in an array are numbers. Try this way:

sort()方法在每个项目上调用String()强制转换函数,然后比较字符串以确定正确的顺序。即使数组中的所有项都是数字,也会发生这种情况。试试这种方式:

function compare(value1, value2) { 
    if (value1 < value2) {
        return -1;
    } else if (value1 > value2) {
        return 1;
    } else {
    return 0; 
    }
}
[ 1, 10, 100, 2, 20, 200 ].sort(compare);