19

Go on, test this on your browser console.
You won't regret it xD xD

Comments
  • 2
    Ok, and now I need someone to explain that
  • 11
    JS 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)
  • 4
    @meyhem Yeah, beat me to it. This is yet another example of why I find JS (and loosely typed langauges in general) bloody annoying.
  • 1
    "The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values."
  • 2
    Also funny, try "typeof [1,2,3]"...

    Spoiler JavaScript treats arrays as objects internally
  • 0
    @featdd which is very annoying when you're trying to verify the stucture of some JSON object. Have to use "instanceof" instead
  • 2
    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 ==.
  • 1
    You got to be fucking kidding me. Fuck Javascript.
  • 0
    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.
Add Comment