2programminghumor·Programming Humorby2e8a9d6bb6935a54 Javascript sort is the best sortView original on lemmy.world84Comments9
ppoopsmith lemmy.world1Hide 1 replyFrom 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)18
__sideffect lemmy.world2Hide 2 repliesImagine going for a JavaScript coding interview and failing because of this 😂15
sskates replysh.itjust.worksThat'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.4
EEatMyDick replylemmy.worldMeh, it's a day 2 thing to understand how shitty JS deals with numbers. Good devs will cover their asses by default.-3
Didn't belive it. tried it. died a little more inside.
From MDN:
So, if you're trying to sort numbers,
[6, -2, 2, -7].sort((a, b) => a - b)That might be explainable, but doesn't make it logical or acceptable.
Its converting numbers to strings.
Yes. Asute observation captain.
Imagine going for a JavaScript coding interview and failing because of this 😂
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.
Meh, it's a day 2 thing to understand how shitty JS deals with numbers. Good devs will cover their asses by default.
Blessed are those that use
Array.sortwithout passing it arguments