The JavaScript sort function which takes a parameter allows one to pass in a function.
For example:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return a - b}) //Array now becomes [7, 8, 25, 41]
How is it that the code
function(a,b)
{
return a - b
}
is interpreted to be ascending? It's supposed to be divided into three cases, <0 , ==0, and >0 ; but how does this make sense when a
and b
can be anything?
Thank You!