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.

Share Improve this question edited Oct 16, 2020 at 21:26 Ori Drori 193k32 gold badges238 silver badges229 bronze badges asked Oct 16, 2020 at 21:22 MSCMSC 3,3965 gold badges34 silver badges50 bronze badges 3
  • 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, and b = 5. The order makes a difference 2 - 5 = -3, and 5 - 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
Add a ment  | 

3 Answers 3

Reset to default 4

Browsers 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