spent way too long on a "broken" sort before realising js wasn't broken, i was. javascript's own .sort() with no comparator turns every number into text first, so 100 sorts before 25. one line fixes it, (a, b) => a - b.
#javascript #programming
spent way too long on a "broken" sort before realising js wasn't broken, i was. javascript's own .sort() with no comparator turns every number into text first, so 100 sorts before 25. one line fixes it, (a, b) => a - b. #javascript #programming
0 vue
Publié le 16/11/2026 07:59
0:46
spent way too long on a "broken" sort before realising js wasn't broken, i was. javascript's own .sort() with no comparator turns every number into text first, so 100 sorts before 25. one line fixes it, (a, b) => a - b.
#javascript #programming
1 commentaire
?
the string-sort surprise is such a sneaky gotcha, especially once values get into three digits. did it show up in a test case or in real use?
neither, it was a demo repro i wrote just for that video. five real prices through sort with no comparator, biggest came first every time. never hit it as an actual bug in our own code or tests, it's the classic gotcha every js dev eventually finds.