Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does points.sort(function(a, b){return a-b}); return -1, 0 or 1?

Tags:

javascript

My difficulty here could be my mathematical illiteracy, but I was trying to sort some numbers in a JavaScript array and this is the solution I found online. It does indeed work, but my question is why?! I would really like to understand this piece of code properly.

The site, W3 Schools says:

You can fix this by providing a function that returns -1, 0, or 1:

var points = [40, 100, 1, 5, 25, 10];

points.sort(function(a,b){return a-b});

Why would only -1, 0 or 1 be returned? I have Googled, and return can return pretty much any value you want.

Again, if this is an incredibly dumb question I apologise.

like image 652
Camarellini Viberg Avatar asked Jul 15 '14 21:07

Camarellini Viberg


4 Answers

It is not bound to be -1, 0, 1 any negative or positive number will do the trick.

But how it works? The sort() method needs to know the relation between each two elements in order to sort them. For each pair (according to the algorithm used) it calls the function then based on the return value, may swap them. Note that a-b returns a negative value if b>a and a positive one if a>b. That leads to an ascending order.

Just for test, replace a-b with b-a and you will get a descending order. You may even make it more complicated, sort them based on (for example) their least significant digit:

a.sort(function() {return (a%10) - (b%10);}
like image 149
Alireza Avatar answered Oct 16 '22 16:10

Alireza


The sort callback has to return

  • a negative number if a < b
  • 0 if a === b
  • a positive number if a > b

Three possible return values are needed because the sort function needs to know whether a is smaller than, equal to, or larger than b in order to correctly position a in the result array.

It is very common to just return -1, 0 and 1 if you working with non-numerical data (I guess that's why W3Schools mentions it). But if you use numerical data, you can simply subtract the values because

  • if a < b then a - b < 0, i.e. a negative number
  • if a === b then a - b === 0, i.e. 0
  • if a > b then a - b > 0, i.e. a positive number

W3Schools is not very precise which is one of the reason why you should avoid it. Use MDN instead.

like image 44
Felix Kling Avatar answered Oct 16 '22 16:10

Felix Kling


If Function(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

If Function(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.

If Function(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first. Function(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.

like image 2
Mitul Panchal Avatar answered Oct 16 '22 16:10

Mitul Panchal


See the specification, http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.14

Don't rely on things that w3schools has to say.

If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

like image 1
Xotic750 Avatar answered Oct 16 '22 18:10

Xotic750