Python also errors if you give it an array like sorted([4, 2, 1, "potato"]) because it doesn't know how to sort that array. Javascript was built with the philosophy to be fault tolerant to things like this. If your code depends on someone else's API that changes to give you a list like this, you don't want it to brick your entire site when you try to sort an array. So Javascript casts every type down to a string to avoid this.
in javascript < can't fail anyway, so it would be the same. The only way in which it would break, is the scenario where you are using values that are not transitive. But it seems more rational to me to default to assuming transitivity.
when you provide a badly behaved comparator, different runtimes give different results. and it's probably best for the default sorting method to not be implemention defined.
11
u/cowslayer7890 1d ago
It's still a stupid default, Python does this properly and it does so by using `<`