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