Javascript Array.sort() 不能正确排序数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7000851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:13:23  来源:igfitidea点击:

Array.sort() doesn't sort numbers correctly

javascript

提问by Some Guy

In Chrome 14, and Firefox 5 (haven't tested other browsers), the following code doesn't sort the numbers correctly:

在 Chrome 14 和 Firefox 5(尚未测试其他浏览器)中,以下代码未正确对数字进行排序:

<script>
a = new Array();
a.push(10);
a.push(60);
a.push(20);
a.push(30);
a.push(100);


document.write(a.sort())
</script>

It returns 10,100,20,30,60

它返回 10,100,20,30,60

I've tried different numbers, and it always acts as if the 0s aren't there and sorts the numbers correctly otherwise. Anyone know why?

我尝试了不同的数字,它总是表现得好像 0 不存在一样,否则会正确地对数字进行排序。有谁知道为什么?

采纳答案by Jason S

I've tried different numbers, and it always acts as if the 0s aren't there and sorts the numbers correctly otherwise. Anyone know why?

我尝试了不同的数字,它总是表现得好像 0 不存在一样,否则会正确地对数字进行排序。有谁知道为什么?

You're getting a lexicographical sort (e.g. convert objects to strings, and sort them in dictionary order), which is the default sort behavior in Javascript:

你得到了一个字典排序(例如将对象转换为字符串,并按字典顺序对它们进行排序),这是 Javascript 中的默认排序行为:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

array.sort([compareFunction])

Parameters

compareFunction

Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element.

array.sort([compareFunction])

参数

比较函数

指定定义排序顺序的函数。如果省略,则根据每个元素的字符串转换按字典顺序(按字典顺序)对数组进行排序。

In the ECMAscript specification (the normative reference for the generic Javascript), ECMA-262, 3rd ed., section 15.4.4.11, the default sort order is lexicographical, although they don't come out and say it, instead giving the steps for a conceptual sort function that calls the given compare function if necessary, otherwise comparing the arguments when converted to strings:

在 ECMAscript 规范(通用 Javascript 的规范参考)中,ECMA-262,第 3 版。, 第 15.4.4.11 节,默认排序顺序是字典序的,虽然他们不出来说出来,而是给出概念排序函数的步骤,该函数在必要时调用给定的比较函数,否则在转换为字符串时比较参数:

13. If the argument comparefn is undefined, go to step 16.
14. Call comparefn with arguments x and y.
15. Return Result(14).
16. Call ToString(x).
17. Call ToString(y).
18. If Result(16) < Result(17), return ?1.
19. If Result(16) > Result(17), return 1.
20. Return +0.

回答by Joseph Marikle

a.sort(function(a,b){return a - b})

These can be confusing.... check this linkout.

这些可能会令人困惑……请查看此链接

回答by Nick Husher

The default sort for arrays in Javascript is an alphabetical search. If you want a numerical sort, try something like this:

Javascript 中数组的默认排序是按字母顺序搜索。如果您想要数字排序,请尝试以下操作:

var a = [ 1, 100, 50, 2, 5];
a.sort(function(a,b) { return a - b; });

回答by Julien Lafont

You can use a sort function :

您可以使用排序功能:

var myarray=[25, 8, 7, 41]
myarray.sort( function(a,b) { return b - a; } );
// 7 8 25 41

Look at http://www.javascriptkit.com/javatutors/arraysort.shtml

看看http://www.javascriptkit.com/javatutors/arraysort.shtml

回答by Samir Adel

try this:

尝试这个:

a = new Array();
a.push(10);
a.push(60);
a.push(20);
a.push(30);
a.push(100);
a.sort(Test)

document.write(a);


function Test(a,b)
{
    return a > b ? true : false;
}