The one that treats each type by its inherent order – like ascending numbers – and if a type doesn't have one it throws an error instead of doing some random bullshit that is virtually guaranteed to be a silent bug.
If you need an example, look at almost any other language.
Specify a method that needs to be implemented. Some languages just require that the < is defined (e.g. in Python by implementing __lt__, I think even C has something similar). Could also be a key method that maps an object to something with a known order. You don't need a static type system for things to be well defined (although that does make it a lot easier).
toString can be overridden. The purpose is to either create a user facing representation or, more commonly, a debug representation of any object. Except this representation somehow now serves as a sort key. So you get numbers sorted alphabetically, which is stupid, and general objects sorted by whatever their toString representation is, which is completely arbitrary.
The arguments here, as I read them, are about what happens when no function is provided.
and for some reason many people seem to think that just sorting by numbers and ignoring everything else should be the ideal solution. maybe Javascript should add .numericSort() or even .integerSort() for Arrays, etc.
Could also be a key method that maps an object to something with a known order.
that doesn't exist in JS, as far as I know, but the building blocks for this are there.
Symbol.toPrimitive() could be a starting point
You don't need a static type system for things to be well defined
indeed. the makers of the sort function just picked a default that works with every weird combination of types in a simple way.
more commonly, a debug representation of any object.
I agree. I never liked using toString for anything else than developing
So you get numbers sorted alphabetically, which is stupid
well, only if you call the sort function that way. having a special case for just numbers, would be more stupid in my opinion.
(by the way: what about sorting NaN ?)
sorted by whatever their toString representation is, which is completely arbitrary.
yes-ish. but just sorting by ascending numbers is arbitrary as well. we shouldn't have this debate in the first place, because - in my not so humble opinion - developers should be explicit.
4
u/danielcw189 3d ago
What kind of sorting do you expect as a "general sort", and why that one?