This question already has an answer here:
这个问题在这里已有答案:
- How to sort an array of integers correctly 15 answers
- 如何正确排序整数数组15个答案
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不存在,否则正确排序数字。谁知道为什么?
5 个解决方案
#1
39
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
的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.
指定定义排序顺序的函数。如果省略,则根据每个元素的字符串转换按字典顺序(按字典顺序)对数组进行排序。
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.
#2
84
a.sort(function(a,b){return a - b})
These can be confusing.... check this link out.
这些可能令人困惑....请查看此链接。
#3
20
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; });
#4
9
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
#5
0
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;
}
#1
39
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
的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.
指定定义排序顺序的函数。如果省略,则根据每个元素的字符串转换按字典顺序(按字典顺序)对数组进行排序。
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.
#2
84
a.sort(function(a,b){return a - b})
These can be confusing.... check this link out.
这些可能令人困惑....请查看此链接。
#3
20
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; });
#4
9
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
#5
0
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;
}