sortfix
v0.1.0
Published
Fixes Array.prototype.sort
Downloads
5
Readme
sortfix
I love JavaScript. But the sort
function is admittedly broken.
[5, 10, 1].sort();
// => [1, 10, 5]
This humble little package "fixes" it, to make it a little more sane.
I won't claim that this is any sort of comprehensive sort implementation. It just makes the default behavior for sort
a little more sensible:
- Arrays of numbers are sorted properly
- Arrays of strings are sorted properly
- For mixed arrays (numbers + strings), numbers come first, then strings
- For everything else (
null
, bools, objects, etc.), I personally don't think sorting withsort()
even makes sense. So the behavior in this case is undefined. (Sorting with a custom comparison function is still supported, of course.)
Here's how you "use" it:
// Array.prototype.sort gets patched in here.
require('sortfix');
[5, 10, 1].sort();
// => [1, 5, 10]
That's it and that's all.