$ npm install sort-array
Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.
const sortArray = require('sort-array')
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 an ascending sort type
> sortArray(DJs, { by: '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' } ]
Sort by slot using a descending sort type
> sortArray(DJs, { by: 'slot', order: 'desc' })
[ { name: 'Chris', slot: 'twilight' },
{ name: 'Trevor', slot: 'twilight' },
{ name: 'Chris', slot: 'morning' },
{ name: 'Rodney', slot: 'morning' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Mike', slot: 'afternoon' }]
Sort by slot in a custom order.
> const slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
> sortArray(DJs, { by: 'slot', order: 'slotOrder', customOrders: { 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' } ]
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')
Kind: Exported function
| Param | Type | Description |
|---|---|---|
| arr | Array |
Input array. |
| [options] | object |
Sort config. |
| [options.by] | Array.<string> |
One or more properties to sort by. |
| [options.order] | Array.<string> |
One or more sort orders. |
| [options.customOrders] | object |
An object containing one or more custom orders. |
| [options.computed] | object |
An object containing one or more computed field functions. |
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:
const arrayify = require('sort-array')
Within Node.js with ECMAScript Module support enabled:
import arrayify from 'sort-array'
Within an modern browser ECMAScript Module:
import arrayify from './node_modules/sort-array/index.mjs'
Old browser (adds window.sortArray):
<script nomodule src="./node_modules/sort-array/dist/index.js"></script>
© 2015-19 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
© 2010 - cnpmjs.org x YWFE | Home | YWFE