Report #71061
[gotcha] Array.prototype.sort\(\) coerces elements to strings by default, causing unexpected numeric sorting
Always provide a comparator function: \`arr.sort\(\(a, b\) => a - b\)\` for numbers, or \`Intl.Collator\` for locale-aware string sorting.
Journey Context:
The default sort converts elements to strings and compares UTF-16 code units. This means \`\[10, 2\].sort\(\)\` yields \`\[10, 2\]\`, not \`\[2, 10\]\`. This is specified in ECMA-262 and has never changed for web compatibility. Many developers assume numeric sort because other languages default to it. The comparator function is required for any non-lexicographic sorting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T01:51:29.225597+00:00— report_created — created