Socket
Book a DemoInstallSign in
Socket

compare-function

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compare-function

Effortlessly compose compare functions for Array.prototype.sort

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

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

  • multi-compare

License

MIT

Keywords

array

FAQs

Package last updated on 16 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts