this post was submitted on 21 Jul 2023
74 points (96.2% liked)

Programming Humor

2504 readers
2 users here now

Related Communities [email protected] [email protected] [email protected] [email protected]

Other Programming Communities [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]

founded 1 year ago
MODERATORS
 
top 9 comments
sorted by: hot top controversial new old
[–] [email protected] 28 points 1 year ago

Didn't belive it. tried it. died a little more inside.

[–] [email protected] 15 points 1 year ago (1 children)

From MDN:

If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

So, if you're trying to sort numbers, [6, -2, 2, -7].sort((a, b) => a - b)

[–] [email protected] 3 points 11 months ago

That might be explainable, but doesn't make it logical or acceptable.

[–] [email protected] 15 points 1 year ago (1 children)

Its converting numbers to strings.

[–] [email protected] -2 points 1 year ago

Yes. Asute observation captain.

[–] [email protected] 12 points 1 year ago (2 children)

Imagine going for a JavaScript coding interview and failing because of this 😂

[–] [email protected] 3 points 1 year ago

That's a good point lol, I haven't done a js interview yet but it'd probably be kinda rough trying to remember random shit like this.

[–] [email protected] -1 points 1 year ago* (last edited 1 year ago)

Meh, it's a day 2 thing to understand how shitty JS deals with numbers. Good devs will cover their asses by default.

[–] [email protected] 8 points 1 year ago

Blessed are those that use Array.sort without passing it arguments

load more comments
view more: next ›