2
[9,2,1,80].sort((a,b) => a>b)
// gives [ 1, 2, 9, 80 ]

[9,2,1,80].sort((a,b) => a<b)
// gives [ 80, 9, 2, 1 ]

Why? I have some code that uses the above comparison function. A comparison function for numbers should be something like (a,b) => a-b. Why the above code is correct, if it is?

Gibezynu Nu
  • 354
  • 6
  • 15
  • 2
    It is not correct; it works by coincidence. – SLaks Dec 18 '18 at 17:12
  • @SLaks a bit of explanation will help mate :) in case of numbers i have seen it is being used at many places. – Code Maniac Dec 18 '18 at 17:14
  • I don't know where you have seen this, @CodeManiac, but I just tried it on chrome and didn't work... – dquijada Dec 18 '18 at 17:16
  • @dquijada https://imgur.com/a/mAZiH3c check here it does work. i know how it works. i was just curious to know why it is not correct. – Code Maniac Dec 18 '18 at 17:19
  • 1
    @CodeManiac: It depends on the JS engine's implementation of `sort()`, and probably also on the exact contents of the array. – SLaks Dec 18 '18 at 17:20
  • @SLaks in the case of numbers only i have seen it works perfectly fine. but ya as you said with the different content it doesen't. – Code Maniac Dec 18 '18 at 17:21
  • I don't get why JavaScript still hasn't added the [`<=>` (spaceship) operator](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php/31298778#31298778). It's pretty standard for duck typing languages as far as I know. – 3limin4t0r Dec 18 '18 at 17:35

2 Answers2

3

It works sometimes - depending on your browser and the input array - because sort expects either a positive number, 0 or a negative number as return value. The expression a>bor a<b returns a boolean which is converted to 0 or 1. 0 means it's equal, so this is where the specific implementation of the browser - how it handles those equal values - you are using comes into play.

https://www.w3schools.com/jsref/jsref_sort.asp

You can also tell equal values are not stable by reading the ECMAScript-spec (that's what Javascript is based on):

The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). [...] If comparefn is not undefined and is not a consistent comparison function for the elements of this array (see below), the sort order is implementation-defined.

Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
  • I added a link to the ECMAScript-spec and a quotation about elements that are equal being unstable. – Christoph Sonntag Dec 18 '18 at 17:25
  • 2
    The relevant part here is probably "If comparefn is not undefined and is not a consistent comparison function for the elements of this array (see below), the sort order is implementation-defined." I changed your link to point directly at the definition of `Array.prototype.sort` – Jared Smith Dec 18 '18 at 17:27
  • @JaredSmith Thanks, I added it to my answer. – Christoph Sonntag Dec 18 '18 at 17:28
  • I still wouldn't rely on an arbitrarily defined sorting function, it's better to use functions which are specified to work consistently. – Teemu Dec 18 '18 at 17:41
1

The > returns only 0 (equal) or 1 (greater), while sort compare function must return negative, zero or positive.that why the > or < work for some value

Taha Azzabi
  • 2,392
  • 22
  • 31
  • 4
    It would be more accurate to say the comparison returns boolean true or false which get implicitly converted to 1 or 0 respectively. – Jared Smith Dec 18 '18 at 17:18