Originally Posted by rnd me
ok, but i explicitly said, twice, that there is no diff when sorting an array of unique strings, like you might get from Object.keys(ob). you posted code sorting an array of objects...
I don't think you're getting my point. It is not up to us to decide whether or not we can violate the interface we have been given. We don't know implementation details and they are subject to change, so we should always conform to the standard we are working with. A standard that explicitly states: Return zero on equal elements. It does not state "but omit this if you know you're working on unique elements" anywhere.
It doesn't matter whether or not there is an example that currently works and shows a failing scenario. The off-chance of this being true (which you show to think of as possible by your ever so little fear/hope of there being a counter-example, even if it's because you say "it's a crazy world") and the chance of browser implementations changing (which would be perfectly acceptable if it doesn't break the ECMAScript standard) is reason enough to not omit the case where you return zero.
You're very loudly trying to not understand the fact that this is simply false:
afaik, the only way for a manual sort function to return zero is when the elements are the same. If you have an array of unique strings, that cannot happen.
Your elements may all be different from each other
, but if I compare an element to itself
, the two elements in question are
equal. And some sorting algorithms do compare elements with themselves – are they implemented in any browser? I don't know, I don't care and it doesn't matter.
In fact, even some implementations of Quicksort show this very behavior and Quicksort, unlike the inefficient Bubblesort (which by the way would not do this), is a widely used sorting algorithm, as you surely know.
This isn't exactly scientific, but sure more proof than you brought up so far: Link