|
|
|
|
|
by stank345
1525 days ago
|
|
I hear what you're saying, but it still seems pretty bonkers to me that if you try to sort an array of numbers it will cast them to strings and sort alphabetically (!): > [1, 2, 10, 3].sort()
[ 1, 10, 2, 3 ]
> And yes, I agree that throwing an error here for no argument would be better here (a linter WILL enforce this), but this is hardly a critical shortcoming of JS's standard libraryI suppose "critical" is debatable but this seems very fundamental and very unexpected to me. > you DEFINITELY do not need a utility library just to use the language (especially for your examples) I hadn't actually considered using a linter to avoid these types of standard library footguns... that's actually a pretty great idea! |
|
But for example, they probably decided that it was more useful defaulting to having a sort order for things that otherwise aren't comparable:
Since null < undefined and undefined < null are both false, then a simple `a < b` comparator wouldn't sort them at all. Same for objects.If you have an array that's a huge mix of random values, from null to undefined to [] to {} and you sort() it, all of those values will now be grouped together by type.
This doesn't seem like a bonkers trade-off.