JSPM

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

Sort an array of objects by any number of fields, in any order.

Package Exports

  • sort-array

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 by any number of fields, in any custom order.

Example

var sortBy = require('sort-array')

sortBy(recordset, columnNames, customOrder) ⇒ Array

Sort an array of objects by one or more fields

Kind: Exported function

Param Type Description
recordset Array.<object> Input array of objects
columnNames string | Array.<string> Column name(s) to sort by
customOrder object Custom sort order definitions. An object where each key is the column name and the value is an array mandating the custom 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)

> a.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

> var slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
> a.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

> a.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' } ]

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