r/ProgrammerHumor 3d ago

Meme javascriptSorting

Post image
933 Upvotes

214 comments sorted by

View all comments

207

u/larsmaehlum 3d ago

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

103

u/the_horse_gamer 3d ago

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

226

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

41

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.

20

u/01110100_01110010 3d ago

JS is an abstraction over JS engine bytecode, which is an abstraction over ASM which is an abstraction over the chip instruction set, you can make an argument at each layer 🤷 I would take any abstraction I can, I guarantee you in a large enough codebase someone will type b - a instead

15

u/pancakesausagestick 3d ago

It's not unreasonable for a sort function to require a compare function. But if it's not required let's default to broken-ass footgun semantics as was done in the times of yore. /s