@tamtamchik/json-deep-sort
v1.5.1
Published
A comprehensive utility for sorting JSON objects by keys.
Maintainers
Readme
JSON Deep Sort sorts object keys throughout nested objects and arrays without mutating the input. It supports ascending and descending order, optional sorting of primitive arrays, ES modules and CommonJS, and includes TypeScript types. The library has no runtime dependencies.
Quick Start
Install with npm:
npm install @tamtamchik/json-deep-sortOr with yarn:
yarn add @tamtamchik/json-deep-sortSort an object:
import { sort } from '@tamtamchik/json-deep-sort';
const data = { b: 2, a: { d: 4, c: 3 } };
console.log(sort(data));
// { a: { c: 3, d: 4 }, b: 2 }Usage
Pass an object, array, or primitive value to sort. It returns the result
synchronously. Object keys are sorted in ascending order by default; array
items keep their order unless primitive array sorting is enabled.
The Quick Start example uses an ES module import. For CommonJS, use require:
CommonJS
const { sort } = require('@tamtamchik/json-deep-sort');
console.log(sort({ b: 'b', a: 'a', c: 'c' }));
// { a: 'a', b: 'b', c: 'c' }Nested Objects and Arrays
Objects inside arrays are sorted recursively, while the array items keep their positions:
const data = {
b: [3, 1, 2],
a: [
{ b: 'b', a: 'a' },
{ d: 'd', c: 'c' },
],
};
console.log(sort(data));
// { a: [{ a: 'a', b: 'b' }, { c: 'c', d: 'd' }], b: [3, 1, 2] }Descending Order
Set the second argument to false to sort keys in descending order at each level:
console.log(sort({ a: 'a', c: 'c', b: 'b' }, false));
// { c: 'c', b: 'b', a: 'a' }Primitive Arrays
Set the third argument to true to sort arrays of strings, numbers, or booleans,
including arrays nested inside objects:
console.log(sort(['b', 'a', 'c'], true, true));
// ['a', 'b', 'c']
console.log(sort([3, 1, 2], false, true));
// [3, 2, 1]
console.log(sort({ values: [3, 1, 2] }, true, true));
// { values: [1, 2, 3] }Numbers use numeric order, strings use localeCompare, and booleans sort as
false before true in ascending order. Arrays containing null, undefined,
or objects retain their item order. Arrays mixing strings, numbers, and booleans
also keep their original order.
Preserved Values
Primitive values pass through unchanged. Values such as Date, RegExp,
functions, Map, and Set retain their original references:
const date = new Date('2023-01-01');
const result = sort({ b: date, a: { z: 'z', y: 'y' } });
console.log(result.a);
// { y: 'y', z: 'z' }
console.log(result.b === date);
// trueAPI Reference
sort<T>(data: T, ascending = true, sortPrimitiveArrays = false): T
| Parameter | Default | Description |
| --------------------- | -------- | ----------------------------------------------------------- |
| data | Required | Object, array, or primitive value to sort. |
| ascending | true | Sort keys and eligible primitive arrays in ascending order. |
| sortPrimitiveArrays | false | Enable sorting of arrays containing primitive values. |
The return type is inferred from the input. Traversed objects and arrays are
rebuilt; preserved values keep their references. JavaScript enumeration rules
keep integer-index keys in numeric order, even when descending order is requested.
String key ordering uses localeCompare and can depend on the runtime locale.
Development Setup
Use Node.js 22 (22.18 or later), Node.js 24 (24.11 or later), or Node.js 26.
The .nvmrc file selects Node.js 24; npm enforces these development versions
through devEngines. CI tests Node.js 22, 24, and 26. These requirements apply
to development tooling; the published library adds no Node.js engine restriction.
Install dependencies:
npm ciRun the checks used in CI:
npm run check
npm run typecheck
npm run build
npm test
npm run test:packageUseful focused commands:
npm run fix # Format files and apply safe lint fixes with Biome.
npm run dev # Rebuild when source files change.
npm run coverage # Run unit tests and generate coverage reports.Biome checks TypeScript, JavaScript, and JSON. Markdown and YAML are reviewed
manually. Coverage runs deterministic unit tests; test:package installs a packed
archive in an isolated consumer and checks CommonJS, ES modules, TypeScript types,
and package contents.
Documentation
- npm package: published versions and installation details.
- API reference: arguments, defaults, and return values.
- Source: public function and exported TypeScript types.
- Unit tests: nested objects, arrays, sort order, and preserved values.
- Package tests: packed files, imports, and consumer types.
- Security policy: vulnerability reporting.
- CI: formatting, lint, build, and test results.
Security Notes
Use acyclic JSON data. Circular references are unsupported, and very deep inputs can exceed the JavaScript call stack. Sorting keys does not validate or sanitize input, and locale-dependent ordering is unsuitable for cryptographic JSON canonicalization.
Report vulnerabilities by emailing [email protected]. See SECURITY.md for the reporting policy.
Contributing
Pull requests are welcome. For major changes, open an issue first to discuss the proposal.
Support
License
MIT.
