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
-
meyhem2236yJS sort by default stringifies all the elements so its same as sorting ['6', '-2', '2', '-7'] hence resulting in lexical sort. To do numeric just do
arr.sort((a, b) => a - b) -
@meyhem Yeah, beat me to it. This is yet another example of why I find JS (and loosely typed langauges in general) bloody annoying.
-
Orek12996y"The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values."
-
featdd736yAlso funny, try "typeof [1,2,3]"...
Spoiler JavaScript treats arrays as objects internally -
Heh... This keeps coming back... Like learn the language stop reposting this dumb meme... You can find odd things in any language.
In python: this will happen
a = 2
b = 2
a is b # True
a = 316
b = 316
a is b # False
Why??? Because of how python deals with small ints. What should you do? LEARN THE LANGUAGE AND USE ==. -
This is logical, suppose it does numeric sort by default, then, in order to do alphabetic sort we would have to write a function to compare strings, which might be difficult. This however, can be easily transformed to numeric sort.
Related Rants
Go on, test this on your browser console.
You won't regret it xD xD
joke/meme
javascript
sorting
sort
js