r/programminghorror
•
u/Leonnee
•
Jun 12 '25
•
1
Javascript Javascript is filled with horror
2.4k
Upvotes
View all comments
Show parent comment
1
u/TorbenKoehn Jun 12 '25
It's not the same
By your idea,
[1, 2, 3, 4]... would already throw the error in JS (numerical sorting is the topic here). But in Ruby that doesn't happen: It uses a<=>function on each element tuple. Similar to calling1.compare(2)or"a".compare("b")respectively. And2.compare("3")throws the error because it can't compare an integer with a stringThis is essentially the same way it works in JS, just that it's not
<=>/.compare, but.toString().localeComparesince JS doesn't have something similar to<=>or aComparableinterface. Maybe in the future, but at no point would someone go and change the.sort()function for it, since it would basically break the web.In JS, you simply pass
compareto the.sort()function, and the default,.toString().localeCompare, can simply work on compare any type as it casts them to strings.It's also often what you want, especially during web development.