this post was submitted on 05 Sep 2023
918 points (98.7% liked)

Programmer Humor

19660 readers
1732 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] jormaig 9 points 1 year ago (1 children)

Because when it's sorting some of them as ints and some of them as strings. JavaScript has implicit conversion to string.

[–] [email protected] 5 points 1 year ago (1 children)

Wrong. JavaScript sort's default comparison function always converts to strings.

[–] jormaig 3 points 1 year ago (1 children)

Only if one of them is a string right? If you have only numbers then it works fine right? Right? (Please say that I'm right 😭)

[–] [email protected] 5 points 1 year ago* (last edited 1 year ago)

No. It always compares by converting to string. I actually think this is more consistent then having different behaviour if you have a string somewhere in your list.

Basically the default comparator is a.sort((a, b) => `${a}` < `${b}` ? -1 : 1).