23 points

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

permalink
report
reply
18 points

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)

permalink
report
reply
3 points

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

permalink
report
parent
reply
16 points

Its converting numbers to strings.

permalink
report
reply
-3 points

Yes. Asute observation captain.

permalink
report
parent
reply
14 points

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

permalink
report
reply
4 points

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.

permalink
report
parent
reply
-3 points
*

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

permalink
report
parent
reply
9 points

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

permalink
report
reply