Urara-Blog/node_modules/.pnpm-store/v3/files/f8/063c5be04fea0d86fed104a4bf711a81f99734f471fa28e8b4efa0281c3488722460b40b638e35d5fac5e6b3b047330afa4f2e262a86bd8526ee22f727ef9d
2022-08-14 01:14:53 +08:00

22 lines
553 B
Text

'use strict';
module.exports = function (items, comparator) {
comparator = comparator
? comparator
: (a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
let stabilizedItems = items.map((el, index) => [el, index]);
const stableComparator = (a, b) => {
let order = comparator(a[0], b[0]);
if (order != 0) return order;
return a[1] - b[1];
};
stabilizedItems.sort(stableComparator);
for (let i = 0; i < items.length; i++) {
items[i] = stabilizedItems[i][0];
}
return items;
};