JavaScript: Sort BigInt identifiers without a subtraction comparator
A subtraction comparator returns a BigInt when both identifiers are BigInts. Array sorting expects a result it can convert to Number, and that implicit conversion throws. Return -1, 0 or 1 from relational comparisons instead.
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 1Run the example
Save as javascript-sort-bigint-identifiers.mjs:
import assert from 'node:assert/strict';
function compareIds(left, right) {
return left < right ? -1 : left > right ? 1 : 0;
}
const ids = [9007199254740993n, 10n, 2n, 9007199254740992n, 10n];
assert.throws(() => ids.toSorted((a, b) => a - b), TypeError);
const ordered = ids.toSorted(compareIds);
assert.deepEqual(ordered, [2n, 10n, 10n, 9007199254740992n, 9007199254740993n]);
assert.equal(ids[0], 9007199254740993n);
assert.equal(compareIds(10n, 10n), 0);
assert.deepEqual([-2n, 0n, -10n].toSorted(compareIds), [-10n, -2n, 0n]);
assert.deepEqual([].toSorted(compareIds), []);
console.log('Ordered:', ordered.map(String).join(', '));
console.log('Original first ID:', String(ids[0]));
console.log('Equal, negative, and empty-input checks: passed');Run:
node javascript-sort-bigint-identifiers.mjsObserved output:
Ordered: 2, 10, 10, 9007199254740992, 9007199254740993
Original first ID: 9007199254740993
Equal, negative, and empty-input checks: passedKeep the identifiers exact
The comparison operates directly on the BigInts. Its result is an ordinary Number representing the ordering, so no large identifier is converted to Number. The two largest IDs differ by one beyond the safe-integer limit and remain distinct.
Converting the identifiers first could lose that distinction. Leaving out the comparator would instead use the default string ordering, putting 10 before 2. The ECMAScript array comparison algorithm specifies conversion of a custom comparator’s result; an implicit BigInt-to-Number conversion is rejected.
Decide what may enter the list
This example supplies only BigInts and uses toSorted to keep the source array unchanged. Validate types before sorting data from elsewhere. A relational comparator can compare some mixed types, which may conceal a conversion mistake instead of rejecting it. For JSON, store large IDs as decimal strings and validate the expected string format before constructing BigInts.
Equal identifiers compare as zero. If objects also carry a secondary priority, add an explicit tie-breaker rather than relying on a difference between equal IDs. Validate external identifier formats and enforce uniqueness separately.
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-sort-bigint-identifiers.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.