Report #104290
[gotcha] Array.sort\(\) sorts numbers as strings by default, causing unexpected order like \[1,10,2,20\]
Always provide a comparator function for numeric sorting: arr.sort\(\(a,b\)=>a-b\). For strings, use localeCompare for locale-aware sorting.
Journey Context:
The default sort converts elements to strings and compares UTF-16 code units. This means numbers are sorted lexicographically. Many beginners assume numeric sort. The fix is simple but often forgotten. Even experienced developers might rely on default when they have mixed types. The tradeoff is that custom comparators can be slower for large arrays, but correctness is paramount. For arrays of objects, ensure you compare the numeric property correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:04:57.800089+00:00— report_created — created