-1

I am getting different output in Chrome 70.0, Chrome 69.0 and Firefox 63.0 for the same code.

var arr = [1,2,43,1233,5546,33,6,11];
arr.sort(() => -1); //[11, 6, 33, 5546, 1233, 43, 2, 1]
arr.sort(() => 1); //[1, 2, 43, 1233, 5546, 33, 6, 11]

Chrome 70.0 chrome 70


Chrome 69.0

chrome 69


Firefox 63.0 firefox 63

Ankit Sinha
  • 1,630
  • 1
  • 20
  • 19
  • You just need `arr.sort()`. Don't pass compare function for numbers array and still you want to do it then return `-1, 0 or 1` – Satpal Nov 22 '18 at 11:55
  • pls post code in text, not image – yelliver Nov 22 '18 at 11:56
  • 1
    Your last question was closed and yet you deleted it and reposted it. Things haven't changed [your comparator is still not suitable for sorting](https://stackoverflow.com/questions/24080785/sorting-in-javascript-shouldnt-returning-a-boolean-be-enough-for-a-comparison). It's inconsistent, hence you have no guarantee that the sorting algorithm is going to give you "correct" output because there is no definition of correct in this case. – VLAZ Nov 22 '18 at 12:03
  • @vlaz I was not able to explain the question really well so I deleted it and asked it again. – Ankit Sinha Nov 22 '18 at 12:05
  • Right, but the answer is still essentially "you are wrong". I actually agree that the close reason was incorrect but only because I think the link I give is the more relevant one as it goes into more detail of what goes wrong when you use an incorrect comparator, rather than [just saying what the correct comparator looks like](https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly). – VLAZ Nov 22 '18 at 12:08
  • Possible duplicate of [Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function?](https://stackoverflow.com/questions/24080785/sorting-in-javascript-shouldnt-returning-a-boolean-be-enough-for-a-comparison) – VLAZ Nov 22 '18 at 12:09
  • @vlaz The question is it's giving different output in the different browser. I am not looking for what's the solution but I want why it's happening. – Ankit Sinha Nov 22 '18 at 12:15
  • @AnkitSinha yes, [the question I linked by Bergi](https://stackoverflow.com/questions/24080785/sorting-in-javascript-shouldnt-returning-a-boolean-be-enough-for-a-comparison) does go into detail as to why a non-tri state comparison algorithm is incorrect for sorting. Actually his example is with dual state - `1`, `0`, but no `-1` but yours is a subclass of that where you don't even return two states. The answer is exactly the same - the sorting algorithm will not be able to function because you're purposefully confusing it by supplying incorrect information. – VLAZ Nov 22 '18 at 12:18
  • In other words, the sorting algorithm is trying to determine where to place two numbers: "should 11 be placed before 6?" you answer "yes" then "should 6 be placed before 11?" you again answer "yes". Now, where should 6 be placed in this case? Furthermore based on your answers, you can't reason about numbers - "is 6 smaller than 11?" - yes. "Is 6 smaller than 1?" - yes. Now what can we say 11 and 1 without directly comparing them? That they are both either equal or larger than 6 which is incorrect. – VLAZ Nov 22 '18 at 12:28

2 Answers2

1

The Chrome 70 changed its sorting algorithm to a stable one. Stable sort algorithms sort identical elements in the same order that they appear in the input [0].

Array.prototype.sort is now stable in @v8js v7.0 / Chrome 70!

Previously, V8 used an unstable QuickSort for arrays with more than 10 elements. Now, we use the stable TimSort algorithm. [1]

[0] https://en.wikipedia.org/wiki/Sorting_algorithm#Stability

[1] https://twitter.com/mathias/status/1036626116654637057?lang=sk

baklazan
  • 814
  • 6
  • 13
0

This is Mathematics problem, because your provided comparator is not a valid general contract, so you cannot expect a "regular result"

if A > B then must B < A
if A > B then must NOT (B > A)
if A > B and B > C then must A > C

But with your provided comparator () => 1, A > B and B > A at the same time, it does not make sense!

FYI, In Java, they have a warning for this & in some cases if you do not follow there can be "Comparison method violates its general contract" exception:

The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y

https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#compare-T-T-

yelliver
  • 5,648
  • 5
  • 34
  • 65
  • indeed. The assumption that `() => -1` will result in reverse order is NOT generally correct. It may be only for some algorithms like bubble sort that will compare an element with every other to find its place. But that assumption is not going to hold up - if the algorithm does a `compare(a, b)` and `compare(b, a)` comparison and comes up with conflicting results (`a` is smaller than `b` and `b` is smaller than `a` at the same time), you can't expect a coherent order. – VLAZ Nov 22 '18 at 12:15