Out of curiosity, what rules apply here exactly?
alert([-Infinity, -1, Infinity, 0, 1].sort());
Outputs: -1, -Infinity, 0, 1, Infinity
JSFiddle: http://jsfiddle.net/8tVGb/
How is it that -Infinity gets sorted between -1 and 0?
If you don't use a custom compare function, sort
always converts the items to strings and orders them lexicographically. Use
….sort(function(a,b){ return a-b; })
See also How to sort an array of integers correctly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With