Socket
Socket
Sign inDemoInstall

compare-func

Package Overview
Dependencies
3
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    compare-func

Get a compare function for array to sort


Version published
Weekly downloads
5.1M
decreased by-0.31%
Maintainers
1
Install size
22.5 kB
Created
Weekly downloads
 

Readme

Source

NPM version Build Status Dependency Status Coverage Status

Get a compare function for array to sort

Install

$ npm install --save compare-func

Usage

var compareFunc = require('compare-func');

// sort by an object property
[{x: 'b'}, {x: 'a'}, {x: 'c'}].sort(compareFunc('x'));
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]

// sort by a nested object property
[{x: {y: 'b'}}, {x: {y: 'a'}}].sort(compareFunc('x.y'));
//=> [{x: {y: 'a'}}, {x: {y: 'b'}}]

// sort by the `x` propery, then `y`
[{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}].sort(compareFunc(['x', 'y']));
//=> [{x: 'b', y: 'a'}, {x: 'b', y: 'b'}, {x: 'c', y: 'c'}]

// sort by the returned value
[{x: 'b'}, {x: 'a'}, {x: 'c'}].sort(compareFunc(function(el) {
  return el.x;
}));
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]

API

compareFunc([property])

Returns a compare function for array to sort

property

Type: string, function or array of either

If missing it sorts on itself.

The string can be a dot path to a nested object property.

  • sort-on - Sort an array on an object property

License

MIT © Steve Mao

Keywords

FAQs

Last updated on 15 May 2020

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc