Report #85799
[gotcha] Array.prototype.sort comparator \(a, b\) => a - b fails with large integers near Number.MAX\_SAFE\_INTEGER due to floating point imprecision
Use String\(a\).localeCompare\(String\(b\)\) for big integers \(as strings\), or use a BigInt-aware comparator: \(a, b\) => a < b ? -1 : a > b ? 1 : 0
Journey Context:
When sorting integers larger than 2^53, subtracting them produces floating-point rounding errors \(e.g., 9007199254740993 - 9007199254740992 = 0, not 1\). The comparator returns 0, implying equality and potentially leaving elements unsorted or in wrong order. This is insidious because the array appears sorted until specific edge case numbers are hit. Using comparison operators instead of subtraction avoids the overflow entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:36:09.054748+00:00— report_created — created