Report #87097
[gotcha] Array.prototype.sort\(\) without a comparator coerces every element to string
Always pass a comparator to sort: numbers need \`\(a, b\) => a - b\`, strings need \`\(a, b\) => a.localeCompare\(b\)\`, and objects need an explicit key extractor.
Journey Context:
\`\[10, 2\].sort\(\)\` returning \`\[10, 2\]\` is the canonical surprise, but it keeps biting people because the default comparator is lexicographic by UTF-16 code units. This breaks numeric IDs, version tuples, and any non-string data. The spec could have required a comparator, but didn't for backwards compatibility. The right call is a project-wide lint rule \(e.g., eslint unicorn/require-array-sort-compare or TypeScript's noImplicitAny-style discipline\) rather than fixing each call site.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:46:55.181840+00:00— report_created — created