Travis build: 1696

This commit is contained in:
30secondsofcode
2018-02-18 17:47:02 +00:00
parent daa44b7e2a
commit aaafc32999
2 changed files with 39 additions and 1 deletions

View File

@ -157,6 +157,7 @@ average(1, 2, 3);
* [`sortedIndexBy`](#sortedindexby)
* [`sortedLastIndex`](#sortedlastindex)
* [`sortedLastIndexBy`](#sortedlastindexby)
* [`stableSort`](#stablesort-)
* [`symmetricDifference`](#symmetricdifference)
* [`symmetricDifferenceBy`](#symmetricdifferenceby)
* [`symmetricDifferenceWith`](#symmetricdifferencewith)
@ -2328,6 +2329,36 @@ sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1
<br>[⬆ Back to top](#table-of-contents)
### stableSort ![advanced](/advanced.svg)
Performs stable sorting of an array, preserving the initial indexes of items when their values are the same.
Does not mutate the original array, but returns a new array instead.
Use `Array.map()` to pair each element of the input array with its corresponding index.
Use `Array.sort()` and a `compare` function to sort the list, preserving their initial order if the items compared are equal.
Use `Array.map()` to convert back to the initial array items.
```js
const stableSort = (arr, compare) =>
arr
.map((item, index) => ({ item, index }))
.sort((a, b) => compare(a.item, b.item) || a.index - b.index)
.map(({ item }) => item);
```
<details>
<summary>Examples</summary>
```js
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### symmetricDifference
Returns the symmetric difference between two arrays.

File diff suppressed because one or more lines are too long