sort-array
Sort an array of objects by any property value, at any depth, in any custom order.
Last updated 7 years ago by 75lb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install sort-array 
SYNC missed versions from official npm registry.

view on npm npm module downloads Build Status Dependency Status js-standard-style

sort-array

Sort an array of objects or primitives, by any property value, in any combindation of ascending, descending, custom or calculated order.

Example

const sortBy = require('sort-array')

sortBy(recordset, sortBy, sortTypes, [namedConfigs]) ⇒ Array ⏏

Kind: Exported function

Param Type Description
recordset Array Input array of objects or primitive values.
sortBy Array.<(string|function())> One or more property expressions to sort by. Expressions may be strings which refer to properties in the input array; they may be strings which refer to properties in the optional namedConfigs.namedComputedProps parameter; or they may be inline functions which dynamically calculate values for each property in the input array.
sortTypes Array.<(string|Array.<*>)> The sort types for each of the sortBy expressions. Values may be 'asc', 'desc', an array of custom values, and strings which refer to properties in the optional namedConfigs.namedCustomOrders parameter.
[namedConfigs] object Provides a means of reusing computed property functions and custom sort types.
[namedConfigs.namedComputedProps] object Key/value pairs, where the keys correspond to strings given in the sortBy property list, and the values are functions which will dynamically calculated values for each property in the input array.
[namedConfigs.namedCustomOrders] object Key/value pairs, where the keys correspond to strings given in the sortTypes list, and the values are arrays of custom values which define the sort type.

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 an ascending sort type

> sortBy(DJs, [ 'slot' ], [ 'asc' ])
[ { 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

> sortBy(DJs, [ 'slot' ], [ '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 using an 'inline' custom sort type

> sortBy(DJs, [ 'slot' ], [ 'morning', 'afternoon', 'evening', 'twilight' ])
[ { 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 using an 'named' custom sort type

> let namedConfigs = {
    namedCustomOrders: {
      custOrder1: [ 'morning', 'afternoon', 'evening', 'twilight' ]
    }
  }
> sortBy(DJs, [ 'slot' ], [ 'custOrder1' ], namedConfigs)
[ { 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 (with a custom sort type) then name (with an ascending sort type)

> sortBy(DJs, ['slot', 'name'], [ [ 'morning', 'afternoon', 'evening', 'twilight' ], 'asc' ])
[ { name: 'Chris', slot: 'morning' },
  { name: 'Rodney', slot: 'morning' },
  { name: 'Mike', slot: 'afternoon' },
  { name: 'Zane', slot: 'evening' },
  { name: 'Chris', slot: 'twilight' },
  { name: 'Trevor', slot: 'twilight' } ]

© 2015-19 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.

Current Tags

  • 5.1.2                                ...           latest (a month ago)
  • 4.1.0-0                                ...           next (7 years ago)

31 Versions

  • 5.1.2                                ...           a month ago
  • 5.1.1                                ...           a year ago
  • 5.1.0                                ...           a year ago
  • 5.0.0                                ...           2 years ago
  • 4.1.5                                ...           5 years ago
  • 4.1.4                                ...           6 years ago
  • 4.1.3                                ...           6 years ago
  • 4.1.2                                ...           6 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.1.0-0                                ...           7 years ago
  • 4.0.1                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 3.0.5                                ...           7 years ago
  • 3.0.4                                ...           7 years ago
  • 3.0.3                                ...           7 years ago
  • 3.0.2                                ...           7 years ago
  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 3.0.0-3                                ...           7 years ago
  • 3.0.0-2                                ...           7 years ago
  • 3.0.0-1                                ...           7 years ago
  • 3.0.0-0                                ...           7 years ago
  • 1.1.2                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.1.1                                ...           10 years ago
  • 1.1.0                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.1.1                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
  • 0.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 61
Today 0
This Week 8
This Month 46
Last Day 4
Last Week 20
Last Month 2
Dependencies (0)
None
Dev Dependencies (4)

© 2010 - cnpmjs.org x YWFE | Home | YWFE