Shorter stableSort example and test

This commit is contained in:
simov
2018-02-11 15:56:19 +02:00
parent 097ab6ffa1
commit ee6456515c
2 changed files with 8 additions and 73 deletions

View File

@ -10,44 +10,12 @@ test('Testing stableSort', (t) => {
//t.false(stableSort(args..), 'Expected');
//t.throws(stableSort(args..), 'Expected');
// sorted by weight
const input = [
{ height: 100, weight: 80 },
{ height: 90, weight: 90 },
{ height: 70, weight: 95 },
{ height: 100, weight: 100 },
{ height: 80, weight: 110 },
{ height: 110, weight: 115 },
{ height: 100, weight: 120 },
{ height: 70, weight: 125 },
{ height: 70, weight: 130 },
{ height: 100, weight: 135 },
{ height: 75, weight: 140 },
{ height: 70, weight: 140 }
]
// sorted by height (using stableSort)
const target = [
{ height: 70, weight: 95 },
{ height: 70, weight: 125 },
{ height: 70, weight: 130 },
{ height: 70, weight: 140 },
{ height: 75, weight: 140 },
{ height: 80, weight: 110 },
{ height: 90, weight: 90 },
{ height: 100, weight: 80 },
{ height: 100, weight: 100 },
{ height: 100, weight: 120 },
{ height: 100, weight: 135 },
{ height: 110, weight: 115 }
]
const compare = (a, b) => a.height - b.height;
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const compare = () => 0;
// stable
t.deepEqual(stableSort(input, compare), target);
t.deepEqual(stableSort(arr, compare), arr);
// unstable
t.notDeepEqual(input.sort(compare), target);
t.notDeepEqual([...arr].sort(compare), arr);
t.end();
});