From b82e4c5cf1bcf68fa1c1f66b40a04ba1e0351ec5 Mon Sep 17 00:00:00 2001 From: simov Date: Wed, 7 Feb 2018 23:08:16 +0200 Subject: [PATCH] Update test and snippet description with easier to understand examples --- snippets/stableSort.md | 62 ++++++++++++++++++++++++++---- test/stableSort/stableSort.test.js | 44 +++++++++++++++++---- 2 files changed, 90 insertions(+), 16 deletions(-) diff --git a/snippets/stableSort.md b/snippets/stableSort.md index 96b352b08..e02c3b3dc 100644 --- a/snippets/stableSort.md +++ b/snippets/stableSort.md @@ -14,14 +14,60 @@ const stableSort = (arr, compare) => ``` ```js -const str = 'abcdefghijklmnopqrstuvwxyz'; -const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); +// 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 } +]; -// default Array.sort() is unstable in Chrome and NodeJS + modifies the input array -var arr1 = str.split(''); -console.log(arr1.sort(compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // false +const compare = (a, b) => a.height - b.height; -// stable sort + returns new array -const arr2 = str.split(''); -console.log(stableSort(arr2, compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // true +// stable - sorted by height using stableSort() +// Items with the same height are still sorted by weight which means they preserved their relative order. +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 } + ] +*/ ``` diff --git a/test/stableSort/stableSort.test.js b/test/stableSort/stableSort.test.js index 7c41e06f3..cd4a66e9f 100644 --- a/test/stableSort/stableSort.test.js +++ b/test/stableSort/stableSort.test.js @@ -10,16 +10,44 @@ test('Testing stableSort', (t) => { //t.false(stableSort(args..), 'Expected'); //t.throws(stableSort(args..), 'Expected'); - // test if js engine's Array#sort implementation is stable - // https://gist.github.com/leeoniya/5816476 - const str = 'abcdefghijklmnopqrstuvwxyz'; - const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); + // 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 } + ] - const input = str.split(''); - const output = stableSort(input, compare); + // 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 } + ] - t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc'); - t.notDeepEqual(input, output); + const compare = (a, b) => a.height - b.height; + + // stable + t.deepEqual(stableSort(input, compare), target); + // unstable + t.notDeepEqual(input.sort(compare), target); t.end(); }); \ No newline at end of file