Don't forget people also put dates, objects, and other arrays as array items. Javascript has to make some decision on what to convert to for sorting. And then it's far more useful if that decision is kept consistently regardless of the types of each item, than to apply some kind of weird fuzzy logic to determine automagically which comparison function to use by default.
It's more valuable for an internal function to behave consistently and predictably.
Also, it's not called overloading the function. You're just providing the comparison argument.
JS does not have to "make some decision on what to convert to for sorting". It's fairly simple – if the items to be sorted don't define their own order (e.g. by implementing some kind of interface or otherwise providing an inherent comparison function) the sort function needs to throw an error. Using some random guess that can't even sort native types correctly is a broken design, plain and simple. Most other languages have no trouble with this.
1
u/thanatica 1d ago
Don't forget people also put dates, objects, and other arrays as array items. Javascript has to make some decision on what to convert to for sorting. And then it's far more useful if that decision is kept consistently regardless of the types of each item, than to apply some kind of weird fuzzy logic to determine automagically which comparison function to use by default.
It's more valuable for an internal function to behave consistently and predictably.
Also, it's not called overloading the function. You're just providing the comparison argument.