使用 Chrome 对 Javascript 数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1969145/
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
Sorting Javascript Array with Chrome?
提问by jantimon
Is there a way to sort an array using Chrome?
有没有办法使用 Chrome 对数组进行排序?
Using the sort function does not work as seen in this example:
如本例所示,使用 sort 函数不起作用:
var myArray = [1,4,5,3,2];
myArray.sort ( function( a , b ){
return b>a
});
for ( var i = 0; i < myArray.length; i++ )
{
document.write( myArray[i] )
}
Firefox / IE / Opera / Safri output: 54321
Firefox / IE / Opera / Safri 输出: 54321
Chrome output: 53241
铬输出: 53241
Thanks for your time!
谢谢你的时间!
回答by Kobi
This seems standard, return a negative, positive or zero number.
这似乎是标准的,返回负数、正数或零数。
myArray.sort ( function( a , b ){
return a-b;
});
回答by Kshitij Saxena -KJ-
The behavior of Chrome is correct :)
Chrome 的行为是正确的 :)
The ECMA standards require the function being passed to sort() to return a number greater than 0, less than 0 or equal to 0. However, the function you have defined returns true / false. ECMA standards state that for a function which does not behave as expected, the implementation depends on the client.
ECMA 标准要求传递给 sort() 的函数返回大于 0、小于 0 或等于 0 的数字。但是,您定义的函数返回 true/false。ECMA 标准规定,对于未按预期运行的功能,实现取决于客户端。
回答by PedroSouki
Because of what the ECMA Standard covers about sort arrays (in a very simplified way):
由于 ECMA 标准涵盖了有关排序数组的内容(以非常简化的方式):
- If in the comparison receives 1 A descend one position.
- If receives -1 maintain the position and define the superior ranking toward the B.
- If receives 0 does nothing.
- 如果在比较中收到 1 A 下降一位。
- 如果收到 -1 保持位置并定义向 B 的上级排名。
- 如果收到 0 什么都不做。
The safest way to guarantee the same behavior in all browser is :
在所有浏览器中保证相同行为的最安全方法是:
// descending order
abc =[10,2,4,1];
abc.sort(function( a , b ){
return a > b ? -1 : 1;
});
// ascending order
abc.sort(function( a , b ){
return a > b ? 1 : -1;
});
For primitive objects is posible to use the short version
对于原始对象可以使用简短版本
// descending order
abc.sort(function( a , b ){
return b - a;
});
// ascending order
abc.sort(function( a , b ){
return a - b;
});
for objects like:
对于像这样的对象:
var items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 27 },
{ name: 'And', value: 31 },
{ name: 'The', value: -12 },
{ name: 'Zeros', value: 37 },
{ name: 'Magnetic', value: 37 }
The right way is:
正确的方法是:
items.sort(function( a , b ){
var result = a == b ? 0 : b > a ? -1 : 1
if(result === 0)
{
// implement a tight break evaluation
}
return result ;
});
This is the right way because the way that the browser iterates is not defined in the ECMA standard and browser may iterate in different ways. For instance most browsers iterate from top to bottom, but chrome iterates the 1st element with the last and go the way up. So in case of a tight may result different result of most browsers.
这是正确的方式,因为浏览器迭代的方式在 ECMA 标准中没有定义,浏览器可能以不同的方式进行迭代。例如,大多数浏览器从上到下迭代,但 chrome 用最后一个元素迭代第一个元素,然后向上迭代。所以在紧张的情况下可能会导致大多数浏览器的不同结果。
回答by Shamasis Bhattacharya
I think, the correct reason is here: Sorting an array of objects in Chrome, more specifically, this post.
我认为,正确的原因在这里:在 Chrome 中对对象数组进行排序,更具体地说,这篇文章。
Post reading that, if you feel the need to implement your own array sorting function, you may have a look at: http://en.literateprograms.org/Merge_sort_%28JavaScript%29
阅读后,如果你觉得需要实现自己的数组排序功能,你可以看看:http: //en.literateprograms.org/Merge_sort_%28JavaScript%29

