# sort-keys-recursive

> Sort the keys of an object recursively

Latest version **2.1.10** (published 2023-10-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install sort-keys-recursive
pnpm add sort-keys-recursive
yarn add sort-keys-recursive
bun add sort-keys-recursive
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.10 |
| Published | 2023-10-23 |
| First published | 2015-02-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 2 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Kiko Beats |
| Maintainers | kikobeats |
| Keywords | array, deep, deterministic, key, keys, obj, object, recursive, recursively, sort, stable |

## Links

- npm: https://www.npmjs.com/package/sort-keys-recursive
- Repository: https://github.com/kikobeats/sort-keys-recursive
- Homepage: https://github.com/Kikobeats/sort-keys-recursive
- Issues: https://github.com/Kikobeats/sort-keys-recursive/issues
- npm.io page: https://npm.io/package/sort-keys-recursive

## Dependencies (2)

- [kind-of](https://npm.io/package/kind-of.md) ~6.0.2
- [sort-keys](https://npm.io/package/sort-keys.md) ~4.2.0

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 2.1.10 (latest) — 2023-10-23
- 2.1.9 — 2023-09-05
- 2.1.8 — 2022-11-16
- 2.1.7 — 2022-05-17
- 2.1.6 — 2022-04-11
- 2.1.5 — 2022-04-01
- 2.1.4 — 2022-03-02
- 2.1.3 — 2022-02-24
- 2.1.2 — 2021-10-12
- 2.1.1 — 2021-01-01
- 2.1.0 — 2020-10-14
- 2.0.1 — 2017-07-18
- 2.0.0 — 2017-02-26
- 1.2.1 — 2017-01-06
- 1.2.0 — 2017-01-06
- … 5 more at https://npm.io/package/sort-keys-recursive/versions

## README

# sort-keys-recursive

![Last version](https://img.shields.io/github/tag/Kikobeats/sort-keys-recursive.svg?style=flat-square)
[![Coverage Status](https://img.shields.io/coveralls/Kikobeats/sort-keys-recursive.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/sort-keys-recursive)
[![NPM Status](https://img.shields.io/npm/dm/str-match.svg?style=flat-square)](https://www.npmjs.org/package/str-match)

> Sort the keys of an object recursively.

## Install

```bash
npm install sort-keys-recursive --save
```

## Usage

```js
const sortKeysRecursive = require('sort-keys-recursive')

const object = {
  c: 0,
  a: {
    c: ['c', 'a', 'b'],
    a: 0,
    b: 0
  },
  b: 0
}

const output = sortKeysRecursive(object)

console.log(output)

// {
//   a: {
//     a: 0,
//     b: 0,
//     c: ['a', 'b', 'c']
//   },
//   b: 0,
//   c: 0
// };
```

## API

### sortKeysRecursive(input, [options])

#### input

*Required*<br>
Type: `array`|`object`

The collection to be sorted.

#### options

##### compareFunction

Type: `function`

[Compare function](https://github.com/sindresorhus/sort-keys#compare).

##### ignoreArrayAtKeys

Type: `array`

Don't sort the Array at the specified keys, if any.

##### ignoreObjectAtKeys

Type: `array`

Don't sort the Object at the specified keys, if any.

## Examples 

### <code>ignoreArrayAtKeys</code> and <code>ignoreObjectAtKeys</code>

```js

const options = {
  ignoreArrayAtKeys: [ // Don't sort the Array at the specified keys, if any.
    'b'
  ],
  ignoreObjectAtKeys: [ // Don't sort the Object at the specified keys, if any.
    'a'
  ]
}

const input = {
  a: { // This Object will not be sorted.
    a: 'a',
    b: 'b',
    c: 'c',
    d: ['a', 'c', 'b']
  },
  b: ['a', 'c', 'b'], // This Array will not be sorted.
  d: ['a', 'c', 'b']
}

const output = sort(object, options)

console.log(output)

// {
//   a: {
//     a: 'a',
//     b: 'b',
//     c: 'c',
//     d: ['a', 'c', 'b']
//   },
//   b: ['a', 'c', 'b'],
//   d: ['a', 'b', 'c']
// }
```

### <code>compareFunction</code>

You can pass a custom sort function as <code>compareFunction</code>. This function is passed to Javascript <code>[sort()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)</code>, that sorts in alphabetical order by default. The custom function should return zero, a negative or positive value:

```js
const reverseAlphabeticalSort = function (a, b) {
  return a < b
}

const options = {
  compareFunction: reverseAlphabeticalSort
}

const object = {
  a: {
    a: 0,
    c: ['c', 'a', 'b'],
    b: 0
  },
  c: 0,
  b: 0
}

const output = sort(object, options)

console.log(output)

// {
//   c: 0,
//   b: 0,
//   a: {
//     c: ['c', 'b', 'a'],
//     b: 0,
//     a: 0
//   }
// }
```

## License

MIT © [Kiko Beats](http://www.kikobeats.com)

---
_Source: https://npm.io/package/sort-keys-recursive · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
