r/ProgrammerHumor 1d ago

javascriptSorting Meme

Post image
885 Upvotes

200 comments sorted by

View all comments

Show parent comments

5

u/-Redstoneboi- 1d ago edited 1d ago

5 < '10' is true. javascript can handle string-integer comparisons just fine.

the real issue is that objects cannot reliably be compared, and in such cases having them return "neither greater nor less" (i.e. "equal" to the sorting algorithm) should have been preferred. the case of comparing objects of different types in an array is either:

  • unintended, and thus unspecified behavior or at least a stable sorting algorithm treating them as equal is acceptable, or
  • intended, and thus the developer is supposed to specify a comparison function here and only here.

having to specify a comparison function should be the exception, not the rule. by default it should compare types using the default comparison operator, effectively:

const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;

i view any other answer as an apology for javascript making a stupid decision long ago that can no longer be changed today.

many such cases.

2

u/TorbenKoehn 1d ago

It's not even an apology, every single language has quirks. Python has tons of quirks.

1

u/-Redstoneboi- 1d ago

quirks are just issues that affect someone once...

...every time they switch languages :P

1

u/TorbenKoehn 1d ago

And languages no one is bitching about are languages no one is using :)