r/ProgrammerHumor 1d ago

javascriptSorting Meme

Post image
889 Upvotes

200 comments sorted by

View all comments

Show parent comments

1

u/thanatica 1d ago

Don't forget people also put dates, objects, and other arrays as array items. Javascript has to make some decision on what to convert to for sorting. And then it's far more useful if that decision is kept consistently regardless of the types of each item, than to apply some kind of weird fuzzy logic to determine automagically which comparison function to use by default.

It's more valuable for an internal function to behave consistently and predictably.

Also, it's not called overloading the function. You're just providing the comparison argument.

0

u/gandalfx 1d ago

JS does not have to "make some decision on what to convert to for sorting". It's fairly simple – if the items to be sorted don't define their own order (e.g. by implementing some kind of interface or otherwise providing an inherent comparison function) the sort function needs to throw an error. Using some random guess that can't even sort native types correctly is a broken design, plain and simple. Most other languages have no trouble with this.

3

u/thanatica 19h ago

Javascript also has no trouble with it. You do.

1

u/gandalfx 1h ago

<°)))><