# sort-on

> Sort an array on an object property

Latest version **7.0.0** (published 2025-10-07) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2025-10-07 |
| First published | 2015-01-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 1 |
| Unpacked size | 7.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 200 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | sort, sorting, array, by, object, property, dot, path, get |

## Links

- npm: https://www.npmjs.com/package/sort-on
- Repository: https://github.com/sindresorhus/sort-on
- Homepage: https://github.com/sindresorhus/sort-on#readme
- Issues: https://github.com/sindresorhus/sort-on/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/sort-on

## Dependencies (1)

- [dot-prop](https://npm.io/package/dot-prop.md) ^10.1.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 7.0.0 (latest) — 2025-10-07
- 6.1.1 — 2025-08-30
- 6.1.0 — 2024-08-15
- 6.0.0 — 2023-09-30
- 5.2.0 — 2022-09-29
- 5.1.0 — 2022-09-21
- 5.0.0 — 2021-11-17
- 4.1.1 — 2021-06-01
- 4.1.0 — 2019-07-30
- 4.0.0 — 2019-05-21
- 3.0.0 — 2018-01-21
- 2.0.0 — 2017-03-20
- 1.3.0 — 2016-05-01
- 1.2.2 — 2015-06-13
- 1.2.1 — 2015-06-12
- … 5 more at https://npm.io/package/sort-on/versions

## README

# sort-on

> Sort an array on an object property

## Install

```sh
npm install sort-on
```

## Usage

```js
import sortOn from 'sort-on';

// Sort by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], 'x');
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]

// Sort descending by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], '-x');
//=> [{x: 'c'}, {x: 'b'}, {x: 'a'}]

// Sort by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], 'x.y');
//=> [{x: {y: 'a'}}, {x: {y: 'b'}}]

// Sort descending by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], '-x.y');
//=> [{x: {y: 'b'}}, {x: {y: 'a'}}]

// Sort by the `x` property, then `y`
sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['x', 'y']);
//=> [{x: 'b', y: 'a'}, {x: 'b', y: 'b'}, {x: 'c', y: 'c'}]

// Sort by the returned value
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], element => element.x);
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
```

## API

### sortOn(array, property, options)

Returns a new sorted version of the given array.

#### array

Type: `unknown[]`

The array to sort.

#### property

Type: `string | string[] | Function`

The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property.

Prefix it with `-` to sort it in descending order.

#### options

Type: `object`

##### locales

Type: `string | string[]`\
Default: The default locale of the JavaScript runtime.

One or more locales to use when sorting strings.

Should be a locale string or array of locale strings that contain one or more language or locale tags.

If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale.

This parameter must conform to BCP 47 standards. See [`Intl.Collator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator) for more details.

##### localeOptions

Type: [`Intl.CollatorOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options)

Comparison options.

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