compare-func

Get a compare function for array to sort

MIT 12 个版本
安装
npm install compare-func
yarn add compare-func
pnpm add compare-func
bun add compare-func
README

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

版本列表
2.0.0 2020-05-15
1.3.4 2020-05-15
1.3.3 2020-05-15
1.3.2 2016-06-30
1.3.1 2015-06-14
1.3.0 2015-06-13
1.2.0 2015-05-04
1.1.0 2015-05-02
1.0.2 2015-04-03
1.0.1 2015-04-03
1.0.0 2015-04-03
0.0.0 2015-04-03