Add stableSort function
This commit is contained in:
8
test/stableSort/stableSort.js
Normal file
8
test/stableSort/stableSort.js
Normal 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;
|
||||
Reference in New Issue
Block a user