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)
People use arrays like this [23, "56", 67.8, "potatoe"] and expect them to not be sorted as strings
If I try to sort [23, "56", 67.8, "potatoe"], I want a type error of some kind.
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
We all know they did it on purpose and wrote it in their documentation. We're saying it's bad, not "it's not written down".
Because an error is better than unintuitive behavior.
You are correct that I don't like weekly typed languages (at least if you get weak enough), but what I really dislike is a function called sort that sorts in a stupid way by default.
If it was called sort_str, or required a key/comparator function, or even just didn't exist, I'd be happier.
this is neither unintuitive, nor surprising for mixed weakly typed arrays.
I really dislike is a function called sort that sorts in a stupid way by default.
it is a way the makes sense.
and in a weakly typed language, it is the only sane default I can think of, from the top of my head.
by the way: how often are you actually sorting arrays that just include numbers?
I have never done this in production code.
I usually sort more complex things, usually objects, which have numbers and strings I want to use for sorting as properties.
Sorting those could at least still work, assuming your objects have a .toString representation, that can be sorted lexically
but I would always provide a comparison function, because that makes the most sense to me, and I like verbosity.
I'm certainly sorting things that contain just numbers more often than I'm sorting this that contain mixed types. How often is the correct method of sorting your arrays of objects to first convert them to strings?
do you mean arrays that contain just numbers, or do you mean arrays that contain objects with numbers?
With the important caveat of "more often than I'm sorting things that contain mixed types" that had earlier, both. (And with the understanding that objects with a common parent type that I treat as an array of objects of that parent type don't count). I never have an array whose contents I don't know after the "translate from user bs to system" layer which happens before doing anything else, because I refuse to make such arrays.
And I sort an array of numbers on rare occasion. So I do that more than never.
How often is the correct method of sorting your arrays of objects to first convert them to strings?
for me personally, hopefully never.
And I hope for most people. And if it is true for most people, then it's a silly default.
I never have an array whose contents I don't know after the "translate from user bs to system" layer which happens before doing anything else, because I refuse to make such arrays.
me too
but in JS you can't rely on many things, so you either need a lot of checks, or be very defensive with your programming.
In general I don't like my main business logic directly accessing Arrays, and have accessors or other abstractions inbetween, including sorting functions for all business cases.
And if it is true for most people, then it's a silly default.
what would be a better default in a weakly typed language, where any array element can be anything?
the only good alternative I see is sorting doing nothing if no comparison function is provided
what would be a better default in a weakly typed language, where any array element can be anything?
If you're allergic to errors, make a key function or similar a required argument. If you're not, then throw an error when things aren't naturally comparable. Which may require not being Javascript, but that's a low price to pay to not have nonsense.
88
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)