2

I'm questioning everything I know right now.

> [ 37, 4, 3, 1, 3, 10, 8, 29, 9, 13, 19, 12, 11, 14, 20, 22, 22, 27, 28, 33, 34 ].sort((a, b) => a > b)
[19, 34, 3, 1, 3, 10, 8, 29, 9, 13, 4, 12, 11, 14, 20, 22, 22, 27, 28, 33, 37]

Expected it would start with 1, then 3 (not 19 and 34)

Things I've tried:

[ 37, 4, 3, 1, 3, 10, 8, 29, 9, 13, 19, 12, 11, 14, 20, 22, 22, 27, 28, 33, 34 ].sort(function (a, b) { return a > b })

Also:

[ 37, 4, 3, 1, 3, 10, 8, 29, 9, 13, 19, 12, 11, 14, 20, 22, 22, 27, 28, 33, 34 ].sort(function (a, b) { return +a > +b })
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42

3 Answers3

3

Sort numbers array by returning its difference in the compare function.

console.log(
  [37, 4, 3, 1, 3, 10, 8, 29, 9, 13, 19, 12, 11, 14, 20, 22, 22, 27, 28, 33, 34].sort((a, b) => a - b)
)

Check MDN docs for understanding what the return value means, the following is taken from MDN docs:

  1. If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
  2. If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
  3. If compareFunction(a, b) is greater than 0, sort b to a lower index than a. compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

The .sort() callback function is supposed to return a numeric response:

  • -1 (or any negative number) when a < b
  • 0 when a == b
  • 1 (or any positive number) when a > b

Your callback is returning a boolean result, which will be interpreted by the sorting code as either 0 or 1. Thus your callback will give contradictory results for some pairs of values.

As an example, consider the pair from your array 37 and 4. If the callback is invoked with a being 37 and b being 4, your callback will return true (1). That, first of all, is backwards of what it should be. What's more, if later in the sorting process those two values are compared again (which is quite possible), but in the reverse order, your callback returns 0, which is again the wrong answer and also a different wrong answer.

The simplest thing to do in your case is to replace that comparison with simple numeric subtraction:

 (a, b) => a - b
Pointy
  • 405,095
  • 59
  • 585
  • 614
2

To sort numerically you need to add a method which handles numeric sorts.

var array=[ 37, 4, 3, 1, 3, 10, 8, 29, 9, 13, 19, 12, 11, 14, 20, 22, 22, 27, 28, 33, 34 ];
console.log(array.sort(callback));
function callback(a,b){
    return a-b;
}

The compare function has the following form:

function compare(a, b) {
  if (a < b) {
    return -1;
  }
  if (a > b) {
    return 1;
  }
  // a must be equal to b
  return 0;
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128