Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
osx947136yActually the answer it's not wrong.
The fact is just that sort method doesn't work like we think at the first guess. -
@Shahriar JS does not have function to sort numbers, but it does have one for sorting strings. Since it is weakly typed language it will do implicit cast and sort strings.
You can define your own compare functions (for example to sort numbers) and then it will work in a way that you wrote. You can see some nice examples here: https://developer.mozilla.org/en-US...
Btw. Same thing happens when you do "5"+4="54" (there is + operator for strings) and "5"-4=1 (there is no - operator for strings, but there is one for numbers). There are benefits to weakly typed languages, but also negatives if you do not know language you use well enough. -
yeah, ok MDN, but hell they write a whole novel of docs when in 99% of cases all you need is just to pass a simple anonymous fn.
numArray.sort((a,b) => a-b); //does the job -
Good to know that JavaScript doesn't have a sense of what integers are. Thanks.
This is why some people shit on weakly typed languages, they just assume things that aren't always true. -
It literally takes 10 seconds to write a comparator function to sort it like necessary. But no, let's blame a dynamic typed language for being dynamic 🤦♂️
-
@faheel thanks for the tip. 😊
That's not exactly what's op doing though. His array only contains numbers. The default comparer treat them as strings. So no cross type comparison.
Related Rants
God save us all from JS 😑
joke/meme
funny
javascript
god
js