Update test and snippet description with easier to understand examples

This commit is contained in:
simov
2018-02-07 23:08:16 +02:00
parent 5470a0d070
commit c04e0a8a75
2 changed files with 90 additions and 16 deletions

View File

@ -14,14 +14,60 @@ const stableSort = (arr, compare) =>
``` ```
```js ```js
const str = 'abcdefghijklmnopqrstuvwxyz'; // sorted by weight
const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); 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 }
];
// default Array.sort() is unstable in Chrome and NodeJS + modifies the input array const compare = (a, b) => a.height - b.height;
var arr1 = str.split('');
console.log(arr1.sort(compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // false
// stable sort + returns new array // stable - sorted by height using stableSort()
const arr2 = str.split(''); // Items with the same height are still sorted by weight which means they preserved their relative order.
console.log(stableSort(arr2, compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // true stableSort(input, compare);
/*
[
{ 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 }
]
*/
// unstable - sorted by height using Array.sort()
input.sort(compare);
/*
[
{ height: 70, weight: 140},
{ height: 70, weight: 95 },
{ height: 70, weight: 125 },
{ height: 70, weight: 130 },
{ height: 75, weight: 140 },
{ height: 80, weight: 110 },
{ height: 90, weight: 90 },
{ height: 100, weight: 100 },
{ height: 100, weight: 80 },
{ height: 100, weight: 135 },
{ height: 100, weight: 120 },
{ height: 110, weight: 115 }
]
*/
``` ```

View File

@ -10,16 +10,44 @@ test('Testing stableSort', (t) => {
//t.false(stableSort(args..), 'Expected'); //t.false(stableSort(args..), 'Expected');
//t.throws(stableSort(args..), 'Expected'); //t.throws(stableSort(args..), 'Expected');
// test if js engine's Array#sort implementation is stable // sorted by weight
// https://gist.github.com/leeoniya/5816476 const input = [
const str = 'abcdefghijklmnopqrstuvwxyz'; { height: 100, weight: 80 },
const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); { 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 }
]
const input = str.split(''); // sorted by height (using stableSort)
const output = stableSort(input, compare); 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 }
]
t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc'); const compare = (a, b) => a.height - b.height;
t.notDeepEqual(input, output);
// stable
t.deepEqual(stableSort(input, compare), target);
// unstable
t.notDeepEqual(input.sort(compare), target);
t.end(); t.end();
}); });