You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # array-sort [![NPM version](https://img.shields.io/npm/v/array-sort.svg?style=flat)](https://www.npmjs.com/package/array-sort) [![NPM monthly downloads](https://img.shields.io/npm/dm/array-sort.svg?style=flat)](https://npmjs.org/package/array-sort) [![NPM total downloads](https://img.shields.io/npm/dt/array-sort.svg?style=flat)](https://npmjs.org/package/array-sort) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/array-sort.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/array-sort) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/array-sort.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/array-sort)
  2. > Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save array-sort
  7. ```
  8. Install with [yarn](https://yarnpkg.com):
  9. ```sh
  10. $ yarn add array-sort
  11. ```
  12. ## Usage
  13. Sort an array by the given object property:
  14. ```js
  15. var arraySort = require('array-sort');
  16. arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo');
  17. //=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]
  18. ```
  19. **Reverse order**
  20. ```js
  21. arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true});
  22. //=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]
  23. ```
  24. ## Params
  25. ```js
  26. arraySort(array, comparisonArgs);
  27. ```
  28. * `array`: **{Array}** The array to sort
  29. * `comparisonArgs`: **{Function|String|Array}**: One or more functions or object paths to use for sorting.
  30. ## Examples
  31. **[Sort blog posts](examples/blog-posts.js)**
  32. ```js
  33. var arraySort = require('array-sort');
  34. var posts = [
  35. { path: 'c.md', locals: { date: '2014-01-09' } },
  36. { path: 'a.md', locals: { date: '2014-01-02' } },
  37. { path: 'b.md', locals: { date: '2013-05-06' } },
  38. ];
  39. // sort by `locals.date`
  40. console.log(arraySort(posts, 'locals.date'));
  41. // sort by `path`
  42. console.log(arraySort(posts, 'path'));
  43. ```
  44. **[Sort by multiple properties](examples/multiple-props.js)**
  45. ```js
  46. var arraySort = require('array-sort');
  47. var posts = [
  48. { locals: { foo: 'bbb', date: '2013-05-06' }},
  49. { locals: { foo: 'aaa', date: '2012-01-02' }},
  50. { locals: { foo: 'ccc', date: '2014-01-02' }},
  51. { locals: { foo: 'ccc', date: '2015-01-02' }},
  52. { locals: { foo: 'bbb', date: '2014-06-01' }},
  53. { locals: { foo: 'aaa', date: '2014-02-02' }},
  54. ];
  55. // sort by `locals.foo`, then `locals.date`
  56. var result = arraySort(posts, ['locals.foo', 'locals.date']);
  57. console.log(result);
  58. // [ { locals: { foo: 'aaa', date: '2012-01-02' } },
  59. // { locals: { foo: 'aaa', date: '2014-02-02' } },
  60. // { locals: { foo: 'bbb', date: '2013-05-06' } },
  61. // { locals: { foo: 'bbb', date: '2014-06-01' } },
  62. // { locals: { foo: 'ccc', date: '2014-01-02' } },
  63. // { locals: { foo: 'ccc', date: '2015-01-02' } } ]
  64. ```
  65. **[Custom function](examples/custom-function.js)**
  66. If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)`Array.sort()` for more details.
  67. ```js
  68. var arr = [
  69. {one: 'w', two: 'b'},
  70. {one: 'z', two: 'a'},
  71. {one: 'x', two: 'c'},
  72. {one: 'y', two: 'd'},
  73. ];
  74. function compare(prop) {
  75. return function (a, b) {
  76. return a[prop].localeCompare(b[prop]);
  77. };
  78. }
  79. var result = arraySort(arr, function (a, b) {
  80. return a.two.localeCompare(b.two);
  81. });
  82. console.log(result);
  83. // [ { one: 'z', two: 'a' },
  84. // { one: 'w', two: 'b' },
  85. // { one: 'x', two: 'c' },
  86. // { one: 'y', two: 'd' } ]
  87. ```
  88. **[Multiple custom functions](examples/custom-functions.js)**
  89. ```js
  90. var arr = [
  91. {foo: 'w', bar: 'y', baz: 'w'},
  92. {foo: 'x', bar: 'y', baz: 'w'},
  93. {foo: 'x', bar: 'y', baz: 'z'},
  94. {foo: 'x', bar: 'x', baz: 'w'},
  95. ];
  96. // reusable compare function
  97. function compare(prop) {
  98. return function (a, b) {
  99. return a[prop].localeCompare(b[prop]);
  100. };
  101. }
  102. // the `compare` functions can be a list or array
  103. var result = arraySort(arr, compare('foo'), compare('bar'), compare('baz'));
  104. console.log(result);
  105. // [ { foo: 'w', bar: 'y', baz: 'w' },
  106. // { foo: 'x', bar: 'x', baz: 'w' },
  107. // { foo: 'x', bar: 'y', baz: 'w' },
  108. // { foo: 'x', bar: 'y', baz: 'z' } ]
  109. ```
  110. ## About
  111. ### Related projects
  112. * [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.")
  113. * [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
  114. * [sort-asc](https://www.npmjs.com/package/sort-asc): Sort array elements in ascending order. | [homepage](https://github.com/jonschlinkert/sort-asc "Sort array elements in ascending order.")
  115. * [sort-desc](https://www.npmjs.com/package/sort-desc): Sort array elements in descending order. | [homepage](https://github.com/jonschlinkert/sort-desc "Sort array elements in descending order.")
  116. * [sort-object](https://www.npmjs.com/package/sort-object): Sort the keys in an object. | [homepage](https://github.com/doowb/sort-object "Sort the keys in an object.")
  117. ### Contributing
  118. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  119. ### Contributors
  120. | **Commits** | **Contributor** |
  121. | --- | --- |
  122. | 10 | [jonschlinkert](https://github.com/jonschlinkert) |
  123. | 4 | [doowb](https://github.com/doowb) |
  124. | 1 | [iamstolis](https://github.com/iamstolis) |
  125. | 1 | [wkevina](https://github.com/wkevina) |
  126. ### Building docs
  127. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  128. To generate the readme, run the following command:
  129. ```sh
  130. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  131. ```
  132. ### Running tests
  133. 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:
  134. ```sh
  135. $ npm install && npm test
  136. ```
  137. ### Author
  138. **Jon Schlinkert**
  139. * [github/jonschlinkert](https://github.com/jonschlinkert)
  140. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  141. ### License
  142. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  143. Released under the [MIT License](LICENSE).
  144. ***
  145. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 11, 2017._