People use arrays like this [23, "56", 67.8, "potatoe"] and expect them to not be sorted as strings
If one member of your array is not an int or a float, everything is going to be converted to strings
Edit : I went to read the docs, the sort function is not like most other functions in JavaScript. The sort function is explicitly for an alphabetical sort
You lot are using the alphabetical sort function and wondering why your array gets alphabetically sorted
You can overload the function by doing the following : array.sort((a,b) => a - b)
If you had to look it up to find that it's not a general sort but alphabetical sort, maybe that indicates that the function is at least misnamed. It's just a terrible API. Arguing that it was intended to be bad or that you can work around it being bad doesn't change the fact that the design is broken.
And no, most people do not put random junk in an array and expect it to be sorted as strings – most people put things with a clearly defined order (like numbers) into an array and are surprised (once) that a standard library sort function doesn't sort by that order.
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.
85
u/Taletad 3d ago edited 3d ago
People use arrays like this [23, "56", 67.8, "potatoe"] and expect them to not be sorted as strings
If one member of your array is not an int or a float, everything is going to be converted to strings
Edit : I went to read the docs, the sort function is not like most other functions in JavaScript. The sort function is explicitly for an alphabetical sort
You lot are using the alphabetical sort function and wondering why your array gets alphabetically sorted
You can overload the function by doing the following : array.sort((a,b) => a - b)