Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
sort-array
Advanced tools
Readme
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
We found that sort-array demonstrated a not healthy version release cadence and project activity because the last version was released 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
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).