npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tamtamchik/json-deep-sort

v1.5.1

Published

A comprehensive utility for sorting JSON objects by keys.

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-sort

Or with yarn:

yarn add @tamtamchik/json-deep-sort

Sort 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);
// true

API 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 ci

Run the checks used in CI:

npm run check
npm run typecheck
npm run build
npm test
npm run test:package

Useful 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.