Report #52500
[gotcha] Array.sort\(\) produces lexicographic order for numbers \(e.g., \[10, 2\] becomes \[10, 2\]\) instead of numeric order
Always provide an explicit comparator: for numbers use \`\(a, b\) => a - b\` \(or safer \`\(a, b\) => \(a > b\) - \(a < b\)\` to avoid overflow\), for strings use \`\(a, b\) => a.localeCompare\(b\)\`, for objects extract fields first
Journey Context:
ECMA-262 specifies that Array.prototype.sort converts elements to strings and compares UTF-16 code units lexicographically if no comparator is provided. This means 10 becomes '10' and 2 becomes '2', and '10' sorts before '2'. This causes subtle bugs in data visualization, financial grids, and game leaderboards. People assume 'sort' implies numeric sort. Alternatives: Always pass a comparator. For numbers, \`a - b\` works for safe integers but can overflow with extreme values \(use explicit comparison\). For internationalized strings, use localeCompare. The default behavior cannot change due to web compatibility since 1997.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:37:02.775722+00:00— report_created — created