r/programminghorror • • Jun 12 '25 • This 1

Javascript Javascript is filled with horror

Post image
2.4k Upvotes

337 comments sorted by

View all comments

Show parent comment

5

u/mediocrobot Jun 12 '25

So your recommendation is to use Array.prototype.sort instead of Array.prototype.toSorted?

-2

u/roughstonerollin Jun 12 '25

That was my recommendation, but I was wrong. This bug exists in both the "sort" and "toSorted" methods, it cannot natively handle integers

4

u/mediocrobot Jun 12 '25

Yeah, unfortunately. It's not really a bug though, it's just how the language works.

All you have to do to support either is to pass a closure as a parameter that defines how you compare two values. If I remember correctly, this closure will sort numbers in ascending order: (a, b) => a - b

2

u/TorbenKoehn Jun 12 '25

It’s not a bug and of course it can handle integers.

Just that the default comparison is like

a.toString().localeCompare(b.toString())

which doesn’t mean you can’t just pass another comparator like

a - b

1

u/roughstonerollin Jun 12 '25 edited Jun 12 '25

> It's not a bug
Hard disagree. Are you telling me it's a feature?
> Of course it can handle integers
No. The "sort" prototype method cannot natively sort integers. You need to pass a comparator function to get it to work (as you point out). Why am I getting downvoted?

2

u/TorbenKoehn Jun 12 '25

What? How is something that was explicitly decided as the default a „bug“? Do you understand what a bug is?

And what is your definition of „natively handling“? The parameter is always there, just that by default you pass (a, b) => String(a).localeCompare(String(b)) implicitly to it because it is the only thing all JS values share: being able to be casted to a string.

1

u/mediocrobot Jun 12 '25

Yeah, it's kind of a stupid default IMO, but it's not a bug. It goes against the principle of least surprise, but it works exactly as designed.

1

u/roughstonerollin Jun 12 '25

Bug: Wildly unexpected behavior

As an engineer, I wouldn't call something a bug unless it was a missed acceptance criteria on a shipped feature. As a product person, I would call anything that is wildly unexpected behavior and confuses 99% of users a bug. I agree with you that the team that created this decided this was an acceptable trade-off.

Native: By default, out of the box, does not require custom code to get to work. I understand there is a default comparator function under the hood that defaults the values to strings. The default comparator idea creates buggy behavior, wildly unexpected, and should not have been an acceptable trade-off in my view.

1

u/TorbenKoehn Jun 13 '25

The "wildly" is extremely exaggerated by you.

Calling (a, b) => a - b "custom code" is an extreme stretch. It's as much "custom code" as calling .sort() is. It allows a lot of flexibility (like descending easily through (a, b) => b - a)

Any programming language has these things and they all come down to preference and use-cases. Comparisons by custom functions are some of the first things people learn in programming.

People that want to sort strings don't have "unexpected behavior". neither does the function magically change it's sorting behavior depending on the values inserterd, which is exactly not "unexpected behavior", but a strictly defined and fix behavior.

You're hitting this problem really out of proportion.

1

u/roughstonerollin Jun 13 '25

We will have to agree to disagree then.

The default behavior of the hidden comparator function is unintuitive, wildly unexpected. It should just always require a comparator function, or the method name should be “sortAsString”

Supporting my case, this post in r/programminghorror that we are both commenting on.

2

u/mediocrobot Jun 12 '25

Oh, if you're wondering how sorting can be handled natively or like-native, TypedArray.prototype.sort can do it. That requires your array to be a fixed size with fixed-size data.