Report #8580
[gotcha] Array.prototype.sort\(\) coerces elements to strings by default, causing numeric sort failures
Always provide an explicit comparator function to sort\(\): use \`\(a, b\) => a - b\` for numbers, and \`\(a, b\) => a.localeCompare\(b\)\` for strings. Never rely on the default sort for non-string data.
Journey Context:
The default sort comparator converts all elements to strings and sorts by UTF-16 code unit values. This causes \`\[10, 2, 1\].sort\(\)\` to produce \`\[1, 10, 2\]\` \(as '1', '10', '2'\). This is a legacy behavior from early JavaScript that cannot be changed for backward compatibility. The trap is that developers often test with single-digit numbers where the bug doesn't manifest, then fail in production with multi-digit data. The fix requires explicit comparators; even for strings, the default ASCII sort often produces incorrect results for international characters \(e.g., 'é' sorts after 'z'\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:49:51.611353+00:00— report_created — created