Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
sort-array
Advanced tools
Sort an array of objects by any property value, at any depth, in any custom order.
The sort-array npm package is a utility for sorting arrays of objects or primitive values. It provides a simple and flexible API to sort arrays based on various criteria, including multiple fields and custom comparator functions.
Sort by Single Field
This feature allows you to sort an array of objects by a single field. In this example, the array of objects is sorted by the 'age' field.
const sortArray = require('sort-array');
const data = [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 }];
const sortedData = sortArray(data, { by: 'age' });
console.log(sortedData);
Sort by Multiple Fields
This feature allows you to sort an array of objects by multiple fields. In this example, the array is first sorted by 'age' and then by 'name'.
const sortArray = require('sort-array');
const data = [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 }];
const sortedData = sortArray(data, { by: ['age', 'name'] });
console.log(sortedData);
Sort in Descending Order
This feature allows you to sort an array of objects in descending order. In this example, the array is sorted by the 'age' field in descending order.
const sortArray = require('sort-array');
const data = [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 }];
const sortedData = sortArray(data, { by: 'age', order: 'desc' });
console.log(sortedData);
Custom Comparator Function
This feature allows you to use a custom comparator function to sort the array. In this example, the array of numbers is sorted in descending order using a custom comparator function.
const sortArray = require('sort-array');
const data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
const sortedData = sortArray(data, { comparator: (a, b) => b - a });
console.log(sortedData);
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a `_.sortBy` function that can be used to sort arrays of objects by one or more fields. Compared to sort-array, Lodash offers a broader set of utilities beyond just sorting.
Underscore is another utility library similar to Lodash, providing a variety of functions for working with arrays, objects, and other data types. It includes an `_.sortBy` function for sorting arrays of objects. Like Lodash, Underscore offers a comprehensive set of utilities in addition to sorting.
array-sort is a lightweight package specifically designed for sorting arrays of objects. It provides a simple API for sorting by one or more fields. Compared to sort-array, array-sort is more focused on sorting and does not include additional utilities.
Sort an array of objects by any property value, at any depth, in any custom order.
Example
const sortBy = require('sort-array')
Array
⏏Kind: Exported function
Param | Type | Description |
---|---|---|
recordset | Array.<object> | Input array of objects |
columnNames | string | Array.<string> | One or more property expressions to sort by, e.g. 'name' or 'name.first' . |
[customOrder] | object | Custom sort order definitions. An object where each key is the property expression and the value is an array specifying the sort order. Example: { importance: [ 'speed', 'strength', 'intelligence' ]} |
Example
with this data
> DJs = [
{ name: 'Trevor', slot: 'twilight' },
{ name: 'Chris', slot: 'twilight' },
{ name: 'Mike', slot: 'afternoon' },
{ name: 'Rodney', slot: 'morning' },
{ name: 'Chris', slot: 'morning' },
{ name: 'Zane', slot: 'evening' }
]
sort by slot
using the default sort order (alphabetical)
> sortBy(DJs, 'slot')
[ { name: 'Mike', slot: 'afternoon' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Chris', slot: 'morning' },
{ name: 'Rodney', slot: 'morning' },
{ name: 'Chris', slot: 'twilight' },
{ name: 'Trevor', slot: 'twilight' } ]
specify a custom sort order for slot
> const slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
> sortBy(DJs, 'slot', { slot: slotOrder })
[ { name: 'Rodney', slot: 'morning' },
{ name: 'Chris', slot: 'morning' },
{ name: 'Mike', slot: 'afternoon' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Trevor', slot: 'twilight' },
{ name: 'Chris', slot: 'twilight' } ]
sort by slot
then name
> sortBy(DJs, ['slot', 'name'], { slot: slotOrder })
[ { name: 'Chris', slot: 'morning' },
{ name: 'Rodney', slot: 'morning' },
{ name: 'Mike', slot: 'afternoon' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Chris', slot: 'twilight' },
{ name: 'Trevor', slot: 'twilight' } ]
sort by nested property values (at any depth) using dot notation (e.g. 'inner.number'
)
> input = [
{ inner: { number: 5 } },
{ inner: { number: 2 } },
{ inner: { number: 3 } },
{ inner: { number: 1 } },
{ inner: { number: 4 } }
]
> sortBy(input, 'inner.number')
[ { inner: { number: 1 } },
{ inner: { number: 2 } },
{ inner: { number: 3 } },
{ inner: { number: 4 } },
{ inner: { number: 5 } } ]
a custom order for a nested property looks like this:
const customOrder = {
'inner.number': [ 1, 2, 4, 3, 5 ]
}
© 2015-17 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
FAQs
Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order
The npm package sort-array receives a total of 139,476 weekly downloads. As such, sort-array popularity was classified as popular.
We found that sort-array demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.