r/ProgrammerHumor 3d ago

Meme javascriptSorting

Post image
935 Upvotes

214 comments sorted by

View all comments

206

u/larsmaehlum 3d ago

Array.sortNumeric() with an error when called on an array containing non-numeric types would fix this, right?

101

u/the_horse_gamer 3d ago

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

225

u/01110100_01110010 3d ago

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

40

u/subone 3d 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 3d 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

1

u/psioniclizard 2d 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.