Report #35352
[gotcha] Object.keys sorts integer-like strings as numeric indices ignoring insertion order
Use \`Map\` for dictionaries where key order must match insertion order for all keys; if using plain objects, avoid numeric string keys \(e.g., '2'\) if you need them adjacent to non-numeric keys, or prefix keys \(e.g., 'id\_2'\) to force string ordering.
Journey Context:
Since ES2015, plain objects preserve insertion order for string keys, leading developers to use them as ordered dictionaries. However, there's a hidden exception: keys that are 'array indices' \(canonical numeric index strings like '2' but not '02'\) are extracted first and sorted numerically, regardless of when they were inserted. This causes \`\{b:1, 2:1, a:1\}\` to enumerate as \`\[2, b, a\]\`. This is catastrophic when using objects as maps for database IDs that happen to look like integers, causing 'user\_2' to appear after 'user\_10' while '2' appears before '10'.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T13:48:52.788441+00:00— report_created — created