Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@y13i/sort-keys

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@y13i/sort-keys

Sort keys of an object.

latest
Source
npmnpm
Version
0.8.3
Version published
Maintainers
1
Created
Source

sort-keys

npm version

Sort the keys of an object.

Installation

npm install @y13i/sort-keys

Usage

import { sortKeys } from "@y13i/sort-keys";

sortKeys(object, option?)

Returns a new object with sorted keys.

sortKeys({ name: "Alice", age: 30, city: "Tokyo" });
// => { age: 30, city: "Tokyo", name: "Alice" }

object

Type: object

The object to sort keys of.

option

Type: object

option.depth

Type: number (1 or greater integer)

Default: Infinity

Limits how many levels deep to recursively sort. 1 sorts only the top-level object; the default Infinity sorts all nested objects and arrays.

sortKeys(
  { version: 1, author: { name: "Alice", email: "alice@example.com" } },
  { depth: 1 }
);
// => { author: { name: "Alice", email: "alice@example.com" }, version: 1 }
// (nested object left unsorted)
option.prioritize.keys

Type: string[]

Keys listed here are moved to the front, in the order given, before the remaining keys are sorted alphabetically.

sortKeys(
  { spec: {}, kind: "Deployment", metadata: {}, apiVersion: "apps/v1" },
  { prioritize: { keys: ["apiVersion", "kind", "metadata"] } }
);
// => { apiVersion: "apps/v1", kind: "Deployment", metadata: {}, spec: {} }
option.prioritize.primitives

Type: boolean

When true, keys with primitive values (numbers, strings, booleans, null, undefined) are sorted before keys with object or array values.

sortKeys(
  { scripts: { build: "tsc" }, name: "my-app", version: "1.0.0", dependencies: { react: "^18.0.0" } },
  { prioritize: { primitives: true } }
);
// => { name: "my-app", version: "1.0.0", dependencies: { react: "^18.0.0" }, scripts: { build: "tsc" } }
option.compare

Type: Function, (object) => (leftKey: string, rightKey: string) => number

Custom sort logic. The outer function receives the object being sorted; the inner comparator receives two key names and returns a number, following the same convention as Array.prototype.sort. When provided, prioritize options are ignored.

sortKeys(
  { createdAt: "2024-01-01", id: 1, title: "Hello" },
  {
    compare: () => (left, right) => left.length - right.length || left.localeCompare(right),
  }
);
// => { id: 1, title: "Hello", createdAt: "2024-01-01" }  (sorted by key length)

CLI

See y13i/sort-keys-cli.

Prior art

Keywords

sort

FAQs

Package last updated on 30 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts