JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 221073
  • Score
    100M100P100Q159651F
  • 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 per month Build Status Dependency Status

sort-array

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

Example

var sortBy = require("sort-array");

sortBy(recordset, columns, customOrder) ⇒ Array

Sort an array of objects by one or more fields

Kind: Exported function

Param Type Description
recordset Array.<object> input array
columns string | Array.<string> column name(s) to sort by
customOrder object specific sort orders, per columns

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 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.