compare-function
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/compare-function package

2.0.0 • Public • Published

compare-function

Effortlessly compose compare functions for Array.prototype.sort. You provide a value accessor function, it handles the a < b ? -1 : a > b ? 1 : 0 dance.

Build status

Install

$ npm install compare-function

Example

var compare = require('compare-function')
 
var songs = [{
  artist: 'Joy Division',
  track: 'Warsaw'
},{
  artist: 'New Order',
  track: 'Blue Monday'
},{
  artist: 'Freebass',
  track: 'Plan B'
}]
 
function artist (song) {
  return song.artist
}
 
songs.sort(compare(artist)).map(artist)
> ['Freebass', 'Joy Division', 'New Order']
 
songs.sort(compare(-1, artist)).map(artist)
> ['New Order', 'Joy Division', 'Freebass']

See also

License

MIT

Package Sidebar

Install

npm i compare-function

Weekly Downloads

9

Version

2.0.0

License

MIT

Last publish

Collaborators

  • michaelrhodes