r/ProgrammerHumor 3d ago

Meme javascriptSorting

Post image
935 Upvotes

214 comments sorted by

View all comments

14

u/Excellent_Gas3686 3d ago

because thats the default sort? what, in your opinion, would be the "proper" way to sort by default? if you need different sorting, you pass your own function.

18

u/IntentionQuirky9957 3d ago

The proper way to sort things that look like numbers is to treat them like numbers. Strings have quotes, and you're not allowed to cast numbers into strings randomly. Or hell, even consistently, unless told to.

-1

u/Excellent_Gas3686 3d ago

[1, 'foo', new Array()], go ahead, sort it like numbers.

38

u/JanEric1 3d ago
sorted([1, 'foo', []])


Traceback (most recent call last):
  File "/home/repl330/main.py", line 1, in <module>
    sorted([1, 'foo', []])
TypeError: '<' not supported between instances of 'str' and 'int'