admin管理员组文章数量:1333478
I have the latest Firefox and Chrome running on Windows 10. I believe the same issue exists on Mac.
When I have a pare function can someone explain to me why when I console.log
a and b, the results in Chrome are the reverse of the results in Firefox. Oddly, though, the sorted result is the same which doesn't make any sense:
var arr = [3, 1, 2];
var sorted_arr = arr.sort(function(a, b) {
console.log("a, b = ", a, b);
return a - b;
});
console.log(sorted_arr);
I have the latest Firefox and Chrome running on Windows 10. I believe the same issue exists on Mac.
When I have a pare function can someone explain to me why when I console.log
a and b, the results in Chrome are the reverse of the results in Firefox. Oddly, though, the sorted result is the same which doesn't make any sense:
var arr = [3, 1, 2];
var sorted_arr = arr.sort(function(a, b) {
console.log("a, b = ", a, b);
return a - b;
});
console.log(sorted_arr);
The first line of the console.log
in Chrome is displayed as a, b = 1 3
whereas in Firefox it is a, b = 3 1
.
- 4 "the sorted result is the same which doesn't make any sense" - why not? Your parison function is only called to determine which of the two elements should be first in the sorting, it tells you nothing about how the sorting is implemented – UnholySheep Commented Oct 16, 2020 at 21:29
- 1 @UnholySheep because I haven't e across any other JS method which either swaps the order of the parameters in different browsers or which essentially handles a +1 the way another browser handles a -1. – MSC Commented Oct 19, 2020 at 2:34
-
So if
a = 2
, andb = 5
. The order makes a difference2 - 5 = -3
, and5 - 2 = 3
. And yet the result is the same, which was the original question. None of the answers on this page actually answer the OPs question. – GN. Commented Sep 22, 2021 at 5:33
3 Answers
Reset to default 4Browsers are allowed to implement sort
however they want, and you should not rely on any particular order of the calls to the parison function.
The answer on this question might help: How does JavaScript's sort (pareFunction) work? [duplicate]
Basically, browsers have no strict way to implement sort
like the other answer here stated. This longer explanation might make it more clear though, as it's a bit more in depth.
The HOST (Browser) when it es to ECMAScript standard does not follow the policy of "implementation defined". This means that the behavior of Array.prototype.sort method may be different between engine implementations. Please refer to official documentation under these links:
https://tc39.es/ecma262/#sec-array.prototype.sort
https://tc39.es/ecma262/#implementation-defined
If you want o achieve a consistent behavior of sorting between browsers that is not obvious you should map the array to an indexed object. In this case both Chrome and Firefox result in same iteration over indexes and values.
var arr = [3,1,2];
var sorted_arr = arr.sort(function(a,b) {
console.log("a, b = ", a, b);
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
// a must be equal to b
return 0;
});
console.log("sorted_arr: ", sorted_arr)
var mapped = arr.map(function(el, i) {
return { index: i, value: el.toString().toLowerCase() };
})
mapped.sort(function(a, b) {
console.log("map: ", a, b)
if (a.value > b.value) {
return 1;
}
if (a.value < b.value) {
return -1;
}
return 0;
});
// container for the resulting order
var result = mapped.map(function(el){
return arr[el.index];
});
console.log("result: ", result)
https://jsfiddle/cw65b4t8/
本文标签: javascriptSortingcompare in Chrome vs FirefoxStack Overflow
版权声明:本文标题:javascript - Sortingcompare in Chrome vs Firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742341553a2456703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论