396 words
2 minutes

JavaScript sort Puts undefined Last: How to Put It First

2026-11-17
Frontend
JavaScript
/
Node.js
/
Troubleshooting
Contents

JavaScript’s sort() puts explicit undefined elements after defined elements, even when your comparator has a branch that would put them first. If you only need to move undefined to the front, partition the array in one pass. If you also need to order the numbers, wrap each value in an object before sorting so the comparator can inspect the value inside it.

Why the undefined branch never runs#

ECMAScript’s comparison operation handles undefined before calling your comparator. Two undefined elements compare as equal. When exactly one element is undefined, it sorts after the defined element. The same comparison rule applies to toSorted(). After wrapping, the array elements are objects, so an undefined field inside an object can reach your comparator. ECMAScript CompareArrayElements

Verify both ways to put undefined first#

Save this as undefined-first.mjs and run node undefined-first.mjs. The fixture was executed during article preparation on macOS with Node.js 24.13.0 on November 17, 2026. It assumes a dense array containing finite numbers and explicit undefined values.

import assert from 'node:assert/strict';
const source = [40, undefined, 3, undefined, 0, -2];
let sawUndefined = false;
function compareValues(a, b) {
if (a === undefined) return b === undefined ? 0 : -1;
if (b === undefined) return 1;
return a - b;
}
function traceComparison(a, b) {
sawUndefined ||= a === undefined || b === undefined;
return compareValues(a, b);
}
const undefinedLast = [-2, 0, 3, 40, undefined, undefined];
assert.deepEqual(source.slice().sort(traceComparison), undefinedLast);
assert.deepEqual(source.toSorted(traceComparison), undefinedLast);
assert.equal(sawUndefined, false);
const sorted = source
.map((value) => ({ value }))
.sort((a, b) => compareValues(a.value, b.value))
.map(({ value }) => value);
assert.deepEqual(sorted, [undefined, undefined, -2, 0, 3, 40]);
const missing = [];
const present = [];
for (const value of source) {
if (value === undefined) {
missing.push(value);
} else {
present.push(value);
}
}
const grouped = missing.concat(present);
assert.deepEqual(grouped, [undefined, undefined, 40, 3, 0, -2]);
assert.deepEqual(source, [40, undefined, 3, undefined, 0, -2]);
console.log('Direct sort, wrapped sort, and stable grouping passed.');

The wrapped sort puts both undefined values first and orders the remaining numbers. The partition preserves the existing order of the numbers, so it also works when that order already reflects a separate ranking. Both approaches leave the input array intact.

If your array has holes, define what they mean before applying either approach. The loop reads ordinary holes as undefined, and the wrapping step uses map(), which skips holes. Inspect slot ownership first if your downstream code needs to preserve sparse array holes.

JavaScript sort Puts undefined Last: How to Put It First
https://laplusda.com/en/posts/javascript-sort-undefined-first/
Author
Zero
Published at
2026-11-17
License
CC BY-NC-SA 4.0