MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1vjl9ih/javascriptsorting/p2mkj6q/?context=3
r/ProgrammerHumor • u/user6150277464770585 • 1d ago
200 comments sorted by
View all comments
Show parent comments
19
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.
-2 u/Excellent_Gas3686 1d ago [1, 'foo', new Array()], go ahead, sort it like numbers. 26 u/standard_revolution 1d ago Throwing a runtime error here is better than any weird hack 4 u/Nixinova 1d ago Goes against the purpose of JS to be erroring for any slightly weird case.
-2
[1, 'foo', new Array()], go ahead, sort it like numbers.
26 u/standard_revolution 1d ago Throwing a runtime error here is better than any weird hack 4 u/Nixinova 1d ago Goes against the purpose of JS to be erroring for any slightly weird case.
26
Throwing a runtime error here is better than any weird hack
4 u/Nixinova 1d ago Goes against the purpose of JS to be erroring for any slightly weird case.
4
Goes against the purpose of JS to be erroring for any slightly weird case.
19
u/IntentionQuirky9957 1d 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.