Add stableSort function

This commit is contained in:
simov
2018-02-06 18:33:49 +02:00
parent 1da19dfa4d
commit cb4f3226f6
4 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,8 @@
var stableSort = (arr, compare) =>
arr
.map((item, index) => ({ item, index }))
.sort((a, b) =>
((result = compare(a.item, b.item)) => (result !== 0 ? result : a.index - b.index))()
)
.map(({ item }) => item);
module.exports = stableSort;