项目作者: jonschlinkert

项目描述 :
Sort an array of objects by property value.
高级语言: JavaScript
项目地址: git://github.com/jonschlinkert/sort-by-value.git
创建时间: 2017-04-27T03:58:24Z
项目社区:https://github.com/jonschlinkert/sort-by-value

开源协议:MIT License

下载


sort-by-value NPM version NPM monthly downloads NPM total downloads Linux Build Status

Re-order the objects in an array so that values of a specified property match the order of a provided array of values.

Install

Install with npm:

  1. $ npm install --save sort-by-value

Install with yarn:

  1. $ yarn add sort-by-value

Usage

  1. var sortBy = require('sort-by-value');
  2. sortBy(array, {
  3. prop: 'foo', // the property name to sort by
  4. values: ['a', 'b', 'c'] // the property values to sort by
  5. });

Example

  1. var arr = [
  2. {title: 'Foo'},
  3. {title: 'Baz'},
  4. {title: 'Bar'},
  5. {title: 'Qux'},
  6. {title: 'Faz'},
  7. {title: 'Fez'}
  8. ];
  9. var res = sortBy(arr, {
  10. prop: 'title',
  11. values: ['Faz', 'Qux', 'Bar'],
  12. normalize: function(val) {
  13. return val;
  14. }
  15. });
  16. console.log(res);
  17. // [ { title: 'Faz' },
  18. // { title: 'Qux' },
  19. // { title: 'Bar' },
  20. // { title: 'Foo' },
  21. // { title: 'Baz' },
  22. // { title: 'Fez' } ]

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Building docs

(This project’s readme.md is generated by verb, please don’t edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

  1. $ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

  1. $ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert.
Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on April 27, 2017.