The default value for options.order is 'asc'. You can also specify 'desc' or the name of a property from the customOrders object. For example, sort parts of the day by the order in which they occur.
Sort by a computed field, i.e. a computed value that doesn't exist in the input dataset. Define your computed fields in the options.computed object, each value being a function which takes an array member as input and returns the primitive value to be sorted by. In this example we sort by total (the name of the computed field supplied in options.computed).
Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.
Example
const sortArray = require('sort-array')
sortArray(array, [options]) ⇒ Array ⏏
Kind: Exported function Returns: Array - Returns the array that was passed in.
Param
Type
Description
array
Array
The input array to sort. It is sorted in place.
[options]
object
Sort options.
[options.by]
Array.<string>
One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects.
[options.order]
Array.<string>
One or more sort orders. Specify 'asc', 'desc' or a property name from the options.customOrders object.
[options.customOrders]
object
A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in.
[options.computed]
object
A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted.
Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js CommonJS:
const sortArray = require('sort-array')
Node.js ECMAScript:
import sortArray from 'sort-array'
Within an modern browser ECMAScript Module:
import sortArray from './node_modules/sort-array/dist/index.mjs'