(Relevant xckd: https://xkcd.com/293/)
(Relevant xckd: https://xkcd.com/293/)
Javascript has some weird behaviors, but in this case Arrays in Javascript can contain multiple types. How would you sort an array of `['cat', 80.3, '80']`? I think I would expect that to end up as `['80', 80.3, 'cat']`, not `[80.3, '80', 'cat'].
Or to make things more complicated, `['cat', 80.3, { foo: 'bar' }, true]`. Do we want to try and intuit whether that object gets converted to a string or a value? Do we base that intuition on the prototype chain, or what?
I think it makes sense to cast every object/type in the array to a shared format so everything can be compared based on the same criteria.
---
Have simple rules that are universally applied:
- All objects are converted to strings
- Strings are sorted alphabetically
vs something weird like:
- We try to intuit what type you want based on several common type conversions and possibly the prototype
- We have a set of built-in rules about which primitive types (number, boolean, string, Symbol) comes before and after other primitive types.
What do YOU expect when you ask someone to sort items that are clearly numbers?
I don't know about you, but I do not expect what actually happened.
Your proposal is that Javascript's default sort algorithm should change behaviors based on whether it can do an extra O(n) loop over the entire array and check whether `typeof x === 'number'` evaluates to true for each index? I don't think that's a good idea.
1 > {} // false
1 < {} // false
So the result of sorting will be random.People are fast to complain..
maybe we could have a `sort2()` method that works differently.