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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ## Stable
  2. A stable array sort, because `Array#sort()` is not guaranteed stable.
  3. MIT licensed.
  4. [![Node.js CI](https://secure.travis-ci.org/Two-Screen/stable.png)](http://travis-ci.org/Two-Screen/stable)
  5. [![Browser CI](http://ci.testling.com/Two-Screen/stable.png)](http://ci.testling.com/Two-Screen/stable)
  6. #### From npm
  7. Install with:
  8. ```sh
  9. npm install stable
  10. ```
  11. Then use it in Node.js or some other CommonJS environment as:
  12. ```js
  13. const stable = require('stable')
  14. ```
  15. #### From the browser
  16. Include [`stable.js`] or the minified version [`stable.min.js`]
  17. in your page, then call `stable()`.
  18. [`stable.js`]: https://raw.github.com/Two-Screen/stable/master/stable.js
  19. [`stable.min.js`]: https://raw.github.com/Two-Screen/stable/master/stable.min.js
  20. #### Usage
  21. The default sort is, as with `Array#sort`, lexicographical:
  22. ```js
  23. stable(['foo', 'bar', 'baz']) // => ['bar', 'baz', 'foo']
  24. stable([10, 1, 5]) // => [1, 10, 5]
  25. ```
  26. Unlike `Array#sort`, the default sort is **NOT** in-place. To do an in-place
  27. sort, use `stable.inplace`, which otherwise works the same:
  28. ```js
  29. const arr = [10, 1, 5]
  30. stable(arr) === arr // => false
  31. stable.inplace(arr) === arr // => true
  32. ```
  33. A comparator function can be specified:
  34. ```js
  35. // Regular sort() compatible comparator, that returns a number.
  36. // This demonstrates the default behavior.
  37. const lexCmp = (a, b) => String(a).localeCompare(b)
  38. stable(['foo', 'bar', 'baz'], lexCmp) // => ['bar', 'baz', 'foo']
  39. // Boolean comparator. Sorts `b` before `a` if true.
  40. // This demonstrates a simple way to sort numerically.
  41. const greaterThan = (a, b) => a > b
  42. stable([10, 1, 5], greaterThan) // => [1, 5, 10]
  43. ```
  44. #### License
  45. Copyright (C) 2018 Angry Bytes and contributors.
  46. Permission is hereby granted, free of charge, to any person obtaining a copy of
  47. this software and associated documentation files (the "Software"), to deal in
  48. the Software without restriction, including without limitation the rights to
  49. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  50. of the Software, and to permit persons to whom the Software is furnished to do
  51. so, subject to the following conditions:
  52. The above copyright notice and this permission notice shall be included in all
  53. copies or substantial portions of the Software.
  54. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  55. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  56. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  57. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  58. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  59. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  60. SOFTWARE.