What is sort-on?
The sort-on npm package allows you to sort an array of objects based on the values of specific properties. It provides a simple and efficient way to perform sorting operations on complex data structures.
What are sort-on's main functionalities?
Sort by a single property
This feature allows you to sort an array of objects by a single property. In this example, the array of objects is sorted by the 'age' property.
const sortOn = require('sort-on');
const data = [
{name: 'John', age: 25},
{name: 'Jane', age: 22},
{name: 'Jim', age: 30}
];
const sortedData = sortOn(data, 'age');
console.log(sortedData);
Sort by multiple properties
This feature allows you to sort an array of objects by multiple properties. In this example, the array is first sorted by 'age' and then by 'height' for objects with the same age.
const sortOn = require('sort-on');
const data = [
{name: 'John', age: 25, height: 180},
{name: 'Jane', age: 22, height: 170},
{name: 'Jim', age: 22, height: 175}
];
const sortedData = sortOn(data, ['age', 'height']);
console.log(sortedData);
Sort by nested properties
This feature allows you to sort an array of objects by nested properties. In this example, the array is sorted by the 'age' property within the 'details' object.
const sortOn = require('sort-on');
const data = [
{name: 'John', details: {age: 25}},
{name: 'Jane', details: {age: 22}},
{name: 'Jim', details: {age: 30}}
];
const sortedData = sortOn(data, 'details.age');
console.log(sortedData);
Other packages similar to sort-on
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data structures. It includes a `_.sortBy` function that can be used to sort an array of objects by one or more properties. Compared to sort-on, Lodash offers a broader range of utilities but may be more complex to use for simple sorting tasks.
array-sort
The array-sort package provides a simple way to sort arrays of objects by one or more properties. It is similar to sort-on in terms of functionality but may have a different API and fewer features. It is a lightweight alternative for basic sorting needs.
sort-array
The sort-array package allows you to sort arrays of objects by one or more properties. It is similar to sort-on but offers additional customization options, such as specifying the sort order (ascending or descending) for each property. It is a good alternative if you need more control over the sorting process.
sort-on
Sort an array on an object property
Install
npm install sort-on
Usage
import sortOn from 'sort-on';
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], 'x');
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], '-x');
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], 'x.y');
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], '-x.y');
sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['x', 'y']);
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], element => element.x);
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.