JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 227292
  • Score
    100M100P100Q159399F
  • License MIT

Sort an array of objects by any property value, at any depth, in any custom order.

Package Exports

  • sort-array
  • sort-array/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sort-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.