Files
30-seconds-of-code/test/stableSort/stableSort.js
2018-02-07 11:17:57 +02:00

6 lines
198 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;