this post was submitted on 14 Aug 2023
672 points (95.2% liked)

Programmer Humor

19594 readers
530 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
[–] Durotar@lemmy.ml 14 points 1 year ago (2 children)

Our default sort converts everything to string, then sorts by UTF-16 code. So yes, [1, 10, 3] is sorted and you are going to live with it.

I'm not sure whether this is satire or not.

[–] Konlanx@feddit.de 58 points 1 year ago* (last edited 1 year ago) (2 children)

It's not. The default sorter does that, because that way it can sort pretty much anything without breaking at runtime. You can overwrite it easily, though. For the example above you could simply do it like this:

[3, 1, 10].sort((a, b) => a - b)

Returns: [1, 3, 10]

[–] newIdentity@sh.itjust.works 5 points 1 year ago

Holy shit that's actually true. I just tried it

[–] sociablefish@programming.dev 2 points 1 year ago

The default sorter does that, because that way it can sort pretty much anything without breaking at runtime.

who the fuck decided that not breaking at runtime was more important than making sense?

this js example of [1, 3, 10].sort() vs [1, 3, 10].sort((a, b) => a - b) will be my go to example of why good defaults are important

[–] sociablefish@programming.dev 1 points 1 year ago

who uses utf 16? people either use utf 8 (for files) or utf 32 (for string class O(1) random access)