Reputation: 47781
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?
Upvotes: 9
Views: 1275
Reputation: 664936
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
Upvotes: 13