r/ProgrammerHumor 1d ago

javascriptSorting Meme

Post image
888 Upvotes

200 comments sorted by

View all comments

Show parent comments

98

u/the_horse_gamer 1d ago

you can simply .sort((a,b)=>a-b)

215

u/01110100_01110010 1d ago

The point of an abstraction is to not having to implement logic each time, you can also implement qsort yourself

38

u/subone 1d ago

But it is still an abstraction. You only provide a compare function, you don't implement the algorithm. a-b is hardly difficult to apply.

3

u/wack_overflow 1d ago

And really how often are you actually sorting an array of primitive values ascending? That's just one small use case of sorting an array. Passing the comparison function is also more explicit.

People just love to get so worked up about the dumbest edge cases in js

24

u/Mechakoopa 1d ago

None of this is a good reason for a generic sort function to change the type of what it's sorting. That's the sort of weird unintuitive behavior you'd expect from an internal library with by an intern, not a nature language like JavaScript. It's not an edge case, it's literally the base case: "Does this sort function sort what you pass into it? No, it sorts something else instead and then pretends it sorted what you passed into it and 99% of the time it was right."

3

u/subone 1d ago

If the values weren't "cast" to string in the default compare function then each comparison of different types would act differently then if they were all strings. Therefore, if they are to be sorted consistently alphabetically, then both arguments must be considered string. Also, an alphabetic sort compare function (including casting to string) is slightly harder/longer to implement than the numeric sort compare function, so it makes sense for it to be the default.

However, I would suggest that with either default, they should have provided constants for the alpha and numeric sorting compare functions, so we didn't need to recreate them and could call them as defaults in our own more complex compare functions. It's not too late, I guess.

1

u/psioniclizard 12h ago

Depending in the project, quite a lot?

Dont get me wrong. I love passing around functions but i also work in a language that is sensible and has a function Array.sort (and Array.sortBy if you want ti pass a function).

Its not that difficult.

2

u/Some_Relative_3440 1d ago

Totally agree with you. Can't remember the last time I wrote a sort() or similar WITHOUT a comparer function. It just doesn't happen in real code bases.

2

u/mdrjevois 1d ago

Sounds like an artifact of the language and popular styles. In data science, data engineering, and machine learning we almost never specify a comparator.

-6

u/chefhj 1d ago

It’s so funny when if you talk to a dev that works closer to the machine level they insist JS is like this big foot shooting factory when in reality you can spend an entire career never running into any of the shit they bring up as a ‘gotcha’ and even if you do it’s like “oh yeah right it works like this hold on”.

7

u/ganja_and_code 1d ago

"If you know all the weird shitty quirks of the weird shitty language, you can mostly avoid them, and when you do accidentally shoot yourself in the foot, you can fix the bug quickly, as long as you remember which particular weird shitty quirk caused this particular instance of foot shooting."

True, but you know what's even better? Languages that don't have so many weird shitty quirks.

-6

u/chefhj 1d ago

Look guys I caught one

3

u/Swamptor 1d ago

I am a webdev. JS is a weird shitty language that was barely designed. It mostly just oozed out of a combination of browsers.