# sort-object

> Sort the keys in an object.

Latest version **3.0.3** (published 2019-09-19) · MIT license · 0 weekly downloads

> **Better alternative:** See documentation for alternatives (https://github.com/AikidoSec/module-replacements/blob/main/docs/sort-object)

## Install

```sh
npm install sort-object
pnpm add sort-object
yarn add sort-object
bun add sort-object
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.3 |
| Published | 2019-09-19 |
| First published | 2013-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 6 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Author | Brian Woodward |
| Maintainers | doowb, jonschlinkert |
| Keywords | arr, array, function, js, key, keys, obj, object, order, re, re-order, sort, util, utils |

## Links

- npm: https://www.npmjs.com/package/sort-object
- Repository: https://github.com/doowb/sort-object
- Issues: https://github.com/doowb/sort-object/issues
- npm.io page: https://npm.io/package/sort-object

## Dependencies (6)

- [bytewise](https://npm.io/package/bytewise.md) ^1.1.0
- [sort-asc](https://npm.io/package/sort-asc.md) ^0.2.0
- [get-value](https://npm.io/package/get-value.md) ^2.0.2
- [sort-desc](https://npm.io/package/sort-desc.md) ^0.2.0
- [union-value](https://npm.io/package/union-value.md) ^1.0.1
- [is-extendable](https://npm.io/package/is-extendable.md) ^0.1.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.0.3 (latest) — 2019-09-19
- 3.0.2 — 2016-02-03
- 3.0.1 — 2016-01-19
- 3.0.0 — 2015-07-21
- 2.0.4 — 2015-07-09
- 2.0.3 — 2015-07-07
- 2.0.2 — 2015-06-30
- 2.0.1 — 2015-06-30
- 2.0.0 — 2015-06-30
- 1.0.0 — 2015-01-30
- 0.3.2 — 2014-10-24
- 0.3.1 — 2014-08-21
- 0.3.0 — 2014-08-21
- 0.2.2 — 2014-08-15
- 0.2.1 — 2014-08-15
- … 7 more at https://npm.io/package/sort-object/versions

## README

# sort-object [![NPM version](https://img.shields.io/npm/v/sort-object.svg)](https://www.npmjs.com/package/sort-object) [![Build Status](https://img.shields.io/travis/doowb/sort-object.svg)](https://travis-ci.org/doowb/sort-object)

> Sort the keys in an object.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm i sort-object --save
```

## Usage

```js
var sortObj = require('sort-object');
```

By default, the keys on an object will be sorted in ascending order:

```js
sortObj({a: 1, c: 2, b: 3});
//=> {a: 1, b: 3, c: 2}
```

The second param can be an object of `options` OR an array of `keys`:

**object**

```js
sortObj({a: 1, c: 2, b: 3}, {keys: ['a', 'b']});
//=> {a: 1, b: 3}
```

**array**

```js
sortObj({a: 1, c: 2, b: 3}, ['a', 'c']);
//=> {a: 1, c: 2}
```

## Options

* `keys` {Array} The returned object will contain only the specified keys, in the same order.
* `sort` {Function} Sort function to sort the keys using JavaScript's `.sort()` method.
* `sortOrder` {String} Valid values are `desc` or `asc`, case insensitive.
* `sortBy` {String} Sort function that is passed the entire object, rather than just the keys - as with the `.sort()` method.

### options.keys

Create a new object with only the given keys.

```js
var o = {a: 1, c: 2, e: 5, d: 4, b: 3};
sortObj(o, {keys: ['a', 'b']});

//=> {a: 1, b: 3}
```

### options.sort

Function to be passed to javascript's `.sort()` method:

```js
var o = {a: 1, c: 2, e: 5, d: 4, b: 3};
var obj = sortObj(o, {
  sort: function (a, b) {
    return a < b ? -1 : 1;
  }
});
obj;
//=> {a: 1, b: 3, c: 2, d: 4, e: 5}
```

### options.sortOrder

Valid values are `desc` or `asc`, case insensitive:

```js
var o = {a: 1, c: 2, e: 5, d: 4, b: 3};
sortObj(o, {sortOrder: 'ASC'});
//=> {e: 5, d: 4, c: 3, b: 2, a: 1}
```

### options.sortBy

Function that returns an array of keys to sort by:

```js
var old = {one: 'aa', two: 'bc', three: 'ab'};
var o = sortObj(old, {
  sortBy: function (obj) {
    var arr = [];
    Object.keys(obj).filter(function(key) {
      if (/^a/.test(obj[key])) arr.push(key);
    });
    return arr.reverse();
  }
});
//=> {three: 'ab', one: 'aa'}
```

## Author
**Brian Woodward**

+ [github/doowb](https://github.com/doowb)
+ [twitter/doowb](http://twitter.com/doowb)

## License
Copyright © 2014-2016 [Brian Woodward](https://github.com/doowb)
Released under the MIT license.

***

_This file was generated by [verb](https://github.com/verbose/verb) on February 03, 2016._

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