compare-function
v2.0.0
Published
Effortlessly compose compare functions for Array.prototype.sort
Downloads
38
Maintainers
Readme
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.
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']