Skip to content
JavaScript: Preserve arrival order for numeric-looking labels

JavaScript: Preserve arrival order for numeric-looking labels

November 19, 2026

An object keyed by numeric-looking labels can change the order in which those labels are listed. If a report must retain arrival order, keep that order in a Map or an explicit sequence instead of deriving it from ordinary object keys.

Set up

Tested on Debian 13 Linux with Node.js v24.19.0. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as javascript-preserve-numeric-label-order.mjs:

import assert from 'node:assert/strict';

const labels = ['10', '2', '01'];
const object = {};
const ordered = new Map();
for (const label of labels) {
  object[label] = `batch-${label}`;
  ordered.set(label, `batch-${label}`);
}
assert.deepEqual(Object.keys(object), ['2', '10', '01']);
assert.deepEqual([...ordered.keys()], labels);
ordered.set('10', 'revised');
assert.deepEqual([...ordered.keys()], labels);
assert.equal(ordered.get('10'), 'revised');
console.log('Object keys:', Object.keys(object).join(', '));
console.log('Map keys after update:', [...ordered.keys()].join(', '));
ordered.delete('10');
ordered.set('10', 'reinserted');
assert.deepEqual([...ordered.keys()], ['2', '01', '10']);
assert.deepEqual([...new Map().keys()], []);
assert.equal(ordered.has(2), false);
assert.equal(ordered.has('2'), true);
console.log('Map keys after reinsertion:', [...ordered.keys()].join(', '));
console.log('Empty map and string-key checks: passed');

Run:

node javascript-preserve-numeric-label-order.mjs

Observed output:

Object keys: 2, 10, 01
Map keys after update: 10, 2, 01
Map keys after reinsertion: 2, 01, 10
Empty map and string-key checks: passed

Object keys have an ordering rule

The labels arrive as 10, 2 and 01. Listing the object’s keys produces 2, 10 and 01. The keys remain strings throughout.

For ordinary objects, ECMAScript’s own-property key ordering puts array-index keys first in ascending numeric order. Other string keys follow their creation order. The leading-zero label in the example does not qualify as an array-index key, so it remains after the other two.

The Map preserves the original label sequence. Updating an existing entry changes its value without moving its position. The Map set algorithm updates an existing key or appends a new entry.

Decide when an entry becomes new again

Deleting and then reinserting the first label moves it to the end of the Map. That is useful if reinsertion means a fresh arrival, but use an in-place value update when the position should stay fixed.

The test keeps labels as strings. A numeric key with the same digits is a different Map key, so normalize the input type before insertion and lookup if labels come from multiple sources. Do not convert a label to Number merely to impose an order: leading zeros may be meaningful, and large identifiers can exceed the safe-integer range.

A Map still stores one value per key. If repeated labels represent separate events, keep an array of records instead of replacing the earlier value. When exporting an ordered report, emit the sequence explicitly; a consumer should not have to infer your report order from object member names.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- javascript-preserve-numeric-label-order.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on