how work sort(function(a, b) {return a-b}) code example

Example: function(a, b){return b - a}

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);}