What is sort-array?
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.
What are sort-array's main functionalities?
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);
Other packages similar to sort-array
lodash
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
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
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-array
Sort an array of objects by any property value, at any depth, in any custom order.
Example
var sortBy = require('sort-array')
sortBy(recordset, columnNames, [customOrder]) ⇒ 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
> var 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:
var customOrder = {
'inner.number': [ 1, 2, 4, 3, 5 ]
}
© 2015-16 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.