Files
30-seconds-of-code/test/stableSort/stableSort.js
2018-08-02 13:49:33 +03:00

7 lines
213 B
JavaScript

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);
module.exports = stableSort;