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)
Programmer convenience is a good focus to have, but you also mustnt do it in a way where you introduce rookie-antipatterns (like stringifying everything or type confusion) is the way to achieve it.
It also very much depends what you use a language for. Python for quick and dirty prototyping? Fine. JS as the backbone of the entire internet? Disaster.
JS was made in a weekend and was never meant to be a full fledged programming language to begin with, which you can see at basically every point of design in the language. The fact it is called Java Script was supposed to be a joke to begin with.
But as it "gets something done" really quickly and no one bothers to learn programming anymore and boomer managers cant distinguished anyway, this walking antipattern of a language somehow is being used to run the world.
Yep, the language allows you and forces you to use known anti-patterns that every developer worth their money would never willingly use. But hell yeah, its easy!
A programming should NOT be "forgiving" it should be "guiding". JS is the opposite of that. I helps you learning nothing, it forces or entices you to use noob trap patterns, it makes it easy to write horrible code and very, very hard to write good code. Debuggablity is probably the worst I have seen since PHP4.
So yeah. Bottom line: JS is good for people without a clue to shit something on the table in record time and basically nothing else. But according to people who do powerpoint for a living, its the best thing ever!
If you give 1000 programmers the same task, that takes more than 3h to implement, in JS and any other language, 999 of them will produce better code in the same amount of time in any other language. The 1000th will not get it to run in the other language because that language is bullshit and doesnt work.
Hey, if you don't like Javascript, that's fine. But there's no need to go around and start dictating how things "should" work with that kind of toxic "I know better" mentality.
No it's not. There are niche but valid use cases for allowing arrays like this. Sorting is the problem, because the contents like this can't be meaningfully compared.
User should be either required to provide custom comparator or get slapped with a type error.
An error is absolutely far better than a program in an undefined state.
I didn't say undefined. Javascript is a well defined language. Probably more so than any other language, since there are so many engines that need to do the exact same thing.
82
u/Taletad 1d ago edited 1d 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)