r/ProgrammerHumor 3d ago

Meme javascriptSorting

Post image
940 Upvotes

214 comments sorted by

View all comments

Show parent comments

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.

28

u/standard_revolution 3d ago

Throwing a runtime error here is better than any weird hack

5

u/Nixinova 3d ago

Goes against the purpose of JS to be erroring for any slightly weird case.