Desk Clarity

A field guide to everyday digital problems.

Developer tools / Practical guide

Numeric Object Keys Change Display Order: Carry an Ordered Entry Array

Integer-index property names are enumerated numerically before other string keys.

By ON Lab2 min readSources checked November 10, 2026 (KST)

Integer-index property names are enumerated numerically before other string keys. If input sequence matters, transmit that sequence explicitly instead of expecting a JSON object to preserve the desired display order.

Reproduce the behavior safely

Run this self-contained fixture in an empty temporary directory, separate from your project. Shell examples create only named fixture files there. JavaScript examples use local values and send no network requests.

const assert = require('node:assert/strict');
const record = JSON.parse('{"12":"first","3":"second","label":"last"}');
assert.deepEqual(Object.keys(record), ['3', '12', 'label']);
const entries = [['12', 'first'], ['3', 'second']];
const ordered = new Map(entries);
assert.deepEqual([...ordered.keys()], ['12', '3']);
assert.deepEqual(JSON.parse(JSON.stringify(entries)), entries);
console.log(JSON.stringify({objectKeys: Object.keys(record), entries}));

Observed local output

{"objectKeys":["3","12","label"],"entries":[["12","first"],["3","second"]]}

Why the result changes

The strings 12 and 3 become property names, and Object.keys exposes their numeric enumeration order. This does not mean every kind of JavaScript key is randomly reordered. The ordered entry array carries a separate sequence; constructing a Map from that array retains it. Constructing the Map from Object.entries after parsing the original object cannot recover sequence already represented differently.

Choose and verify the correction

For event displays and ranked items, make the API return an array of records or pairs with stable identifiers. Render that array directly, or construct a Map from it when key lookup is also needed. Validate duplicate keys before choosing Map, since a later occurrence replaces an earlier value. If the producer cannot change, use an explicit order field or documented sorting policy rather than guessing the intended order from object text.

Limits and next checks

The fixture covers ordinary own enumerable string keys in Node. Symbols, inherited properties and nonenumerable properties have different visibility. Map is not a drop-in JSON wire representation: serialize a chosen array form and reconstruct it deliberately. This ordering issue differs from numeric array sort, where a comparator chooses the order of values already in an array.

Related: JavaScript Sort Puts 10 Before 2: Use a Numeric Comparator and Choose Mutation.

Isolated fixture executed on macOS on November 10, 2026 (KST), with v26.5.0. The observed output verifies this example, not every application or runtime.

Sources & further reading