Report #5801
[gotcha] Array.prototype.sort\(\) produces incorrect numeric order without comparator
Always provide an explicit comparator function: \`\[10, 2\].sort\(\(a, b\) => a - b\)\`. Never rely on default sort for numbers, dates, or mixed types.
Journey Context:
The spec defines that when comparefn is undefined, elements are converted to strings \(UTF-16 code units\) and sorted lexicographically. This means 10 sorts before 2 \("10" < "2"\), and dates sort by their string representation \(often alphabetical day names\) rather than chronological order. This is a spec legacy from early JS. The common mistake is assuming "sort" implies numeric order. The only safe default sort is for already-string arrays where lexicographic order is desired. For everything else, explicitly compare.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T22:13:12.803371+00:00— report_created — created