I know about sorting arrays, but I have the question I'm aching to find out.
For instance:
var array = [9,3,0,-2,15];
So, let's sort it:
function sortFunction(a, b){
if(a < b) return -1; // or any number that less than zero, but why?
if(a > b) return 1; // or any number that above zero, but why?
if(a = b) return 0; // but why?
// I know it may be easier a - b, I've written it for clarity
}
array.sort(sortFunction); //so we'll get correct result
How to understand why it has to return -1, 1, and 0 for sorting array?
P.S. Sorry if question seems stupid, I haven't found the answer in Google.