Report #16368
[gotcha] Array.prototype.sort\(\) coerces elements to strings, causing \[10, 2\].sort\(\) to return \[10, 2\] instead of \[2, 10\]
Always provide an explicit comparator function for non-string arrays: use \`arr.sort\(\(a, b\) => a - b\)\` for numbers, and \`arr.sort\(\(a, b\) => a.localeCompare\(b\)\)\` for locale-aware string sorting.
Journey Context:
JavaScript's default sort converts all elements to strings and compares UTF-16 code units. This is a legacy behavior from early JS specs. Developers expect numeric sorting by default, leading to subtle bugs in financial or index-based calculations. The comparator function \`\(a, b\) => a - b\` is required for numbers. Note that sort stability is guaranteed only since ES2019, but the string coercion remains a persistent footgun.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:27:24.063329+00:00— report_created — created