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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…