r/programminghorror
•
u/Leonnee
•
Jun 12 '25
•
1
Javascript Javascript is filled with horror
2.4k
Upvotes
59
u/[deleted] Jun 12 '25
Well yeah, toSorted defaults to a string sort
909
u/lylesback2 Jun 12 '25
I get JavaScript is filled with horror, but why did you take it out on the poor pixels?
427
u/rover_G Jun 12 '25
I’m now just realizing I’ve never sorted an array in JavaScript
99
u/examinedliving Jun 12 '25
[1,3,10,2].sort((a,b)=>a-b);198
u/patoezequiel Jun 12 '25
Some people love bashing JavaScript like it's the worst.
I've been working with JavaScript for 10 years now.
They are right.
32
u/Ackermannin Jun 12 '25
How actually would you sort an array of integers like that?
134
u/steeltownsquirrel Jun 12 '25
I love it when ints follow lexicographic order! So intuitive!
vomit
9
u/Altruistic-Formal678 Jun 12 '25
Isn't there a website with a quizz full of stupid JS shit like this ? Like the result of Integer.parse(0.0000005) is 7 of stuff like this
24
u/arto64 Jun 12 '25
Posts about these JS quirks are always full of comments calling the OP an idiot for not understanding that, for example, JS by default calls .toString() when sorting an array, like that somehow justifies the horrible language design.
5
u/relmz32 Jun 12 '25
if you didn't want them sorted alphabetically, why would you have them be all ints?
5
u/CuAnnan Jun 12 '25
Lexical sorting, in the docs.
RTFM
4
u/SynthRogue Jun 12 '25
Javascript: "there you go, sorted."
3
u/Hardcorehtmlist Jun 12 '25
This is basic windows counting. 1, 10, 100, 11, 12......19, 20, 200, 21, etc.
That's why I still use 01 or even 001 if need be.
3
u/[deleted] Jun 13 '25
Anyone actually wondering why, the
toSortedmethod takes in an optional compare function that most js devs are already familiar with. Something like:(a, b) => a - bwill produce the desired effect of sorting a list of integers in ascending order.If the compare function isn't passed in, the values to converted to strings and then sorted, giving you what OP's 10 pixels post has.
3
u/PN143 Jun 13 '25
I've never even seen "toSorted()"? .sort() would work correctly and even if it didn't, it can take a comparator function.
Is this sorting the items as if they were strings? (10 still starts with 1, so it's before 2)
10
u/Czebou Jun 12 '25
I mean... How else do you want to sort an array of mixed types? Js is not statically typed, so casting its content to string is a reasonable solution.
Rtfm
2
u/[deleted] Jun 12 '25
lexicographical ordering
2
u/Educational-Agent-32 Jun 12 '25
10 = 2 in binary
2
u/hamarok Jun 12 '25
Thats what you get for not sorting the array yourself.
2
u/nekokattt Jun 12 '25
The pixels, Jesse.
2
u/oweiler Jun 12 '25
There are 20 new JS frameworks born every day, but the jokes/memes will always stay the same
2
u/Sunken_Sunvault Jun 13 '25
Actually it's sorted (in binary tho)
6
u/[deleted] Jun 12 '25
Wait a minute, this isn't even archaic JavaScript from the 1990s that was poorly thought out.
This is from the ECMAScript 2026 specification.
Why did they even do it like this? I thought we were an enlightened species beyond the barbarism of double equals comparison.
7
u/lepapulematoleguau Jun 12 '25
Didn't bother to read the docs did you?
4
u/Thenderick Jun 12 '25
For the millionth time, js was made with questionable design decisions. The main thing being that it shouldn't crash because it would break sites, which is an understandable argument. Arrays allow for multiple different data types instead of one like in a classical sense. You can throw in objects, strings and numbers into one array.
Given the no-crash design decision JS does not want to crash when sorting. The only guarantee it has is that every element can be represented as a string (using the toString() method). And when you want to sort strings you are left with alphabetical order.
Yes it's weird, but it makes sense with that context. JS is weird and has a lot of quirks, but posts like these are low hanging fruit...
3
u/dreamingforward Jun 12 '25 edited Jun 12 '25
It's confirmed. Javascript is like LSD for the internet. It's voodoo.
2
u/InevitableCodeRedo Jun 12 '25
Always was.
2
u/NYJustice Jun 12 '25
Oh no, how dare JS select a default for an operation that makes sense based on their intended use!
I know JS isn't perfect but this is the same joke every CS student posts the second they feel like they have some clout. Send this to your classmates instead, I'm sure they'll love it.
1
u/ETHedgehog- Jun 12 '25
r/countablepixels
1
u/AngriestCrusader Jun 12 '25
r/countablepixels
1
u/ivcrs Jun 12 '25
if (arr.tooSorted())
1
u/disodimani Jun 12 '25
Javashit
1
u/Gyrochronatom Jun 13 '25
...just like a form of cancer that it is.
1
u/Luk164 Jun 13 '25
Got any more of them pixels OP?
1
u/_Kaamaru Jun 13 '25
r/countablepixels
1
u/0xlostincode Jun 13 '25
u/pixel-counter-bot
1
u/theremsoe Jun 15 '25
Please, read the documentation.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
1
u/Emotional-One-9292 Jun 15 '25
We gave up on adobe flash/ActionScript just for that?
1
u/navuyi Jun 15 '25
I mean, the documentation has this exact example in it…
1
u/Achereto Jun 15 '25
It's not wrong, though. I may not be what you expected, but it's most definitely sorted.
1
u/sgt_futtbucker Jun 15 '25
Jesus u/pixel-counter-bot
1
u/Earnestappostate Jun 15 '25
At least it wasn't [1, 10, 3, 2]
(Alphabetical when spelled out.)
1
u/_-_me_-_- Jun 15 '25
u/pixel-counter-bot
1
u/Terrorscream Jun 16 '25
Looks like its just sorting them alphabetically, it's guessing they are strings not numbers is what I'm seeing here, just JS things.
1
u/mortimere Jun 17 '25
thats how linux sorts files sometimes
1
u/conundorum Jun 20 '25
Wow, that's awful. You should sent them a stringly worded essay about their sorting functions!
0
u/overcloseness Jun 12 '25
That’s no horror, why would JavaScript know that you’re wanting to sort a number from lowest to highest in terms of how we read numbers? This script is way too arbitrary. Your output is correct and it’s what you’d expect.
1
u/ardicli2000 Jun 12 '25
This is the case for years for many. Excel has the same issue. If you tell the program that these are strings it will act accordingly.
toSorted expects strings. When it finds numbers converts them into strings and do its job.
If you tell it to subtract them (not sum) it will then act like they are numbers and do its job accordingly.
All is working fine.
This is not strictly typed language. It is doing its job very well.
This is not horror. This is programmer incompetency.
-8
u/d0pe-asaurus Jun 12 '25
It would take an O(n) pass to determine if the array consists of numbers only, to decide between a numerical sort and a lexicographic sort.
0
u/[deleted] Jun 12 '25
numbers.sort(), OP.
0
u/retardedGeek Jun 12 '25
RTFM