sort-on
TypeScript icon, indicating that this package has built-in type declarations

6.0.0 • Public • Published

sort-on

Sort an array on an object property

Install

npm install sort-on

Usage

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 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 for more details.

localeOptions

Type: Intl.CollatorOptions

Comparison options.

Package Sidebar

Install

npm i sort-on

Weekly Downloads

144,007

Version

6.0.0

License

MIT

Unpacked Size

7.34 kB

Total Files

5

Last publish

Collaborators

  • sindresorhus