Spot the Bug answer: The Array.prototype.sort() method, when called without a compare function, sorts elements as strings.
The fix:
Change .sort() to .sort((a, b) => a - b).
Why:
By default, JavaScript’s sort method converts elements to strings and compares them based on their Unicode code point values. This causes ‘100’ to be considered ‘less than’ ‘20’ because ‘1’ comes before ‘2’. Providing a compare function (a - b) ensures numerical sorting.
Oh man, this one gets everyone at some point. It’s like when you’re lining up kids by age but some of them have names that start with ‘A’ and ‘B’ and you accidentally sort by name instead of their birth year.