From ee6456515c6b5e92d1c2d44132b1264efac04a4a Mon Sep 17 00:00:00 2001 From: simov Date: Sun, 11 Feb 2018 15:56:19 +0200 Subject: [PATCH] Shorter stableSort example and test --- snippets/stableSort.md | 41 +++--------------------------- test/stableSort/stableSort.test.js | 40 +++-------------------------- 2 files changed, 8 insertions(+), 73 deletions(-) diff --git a/snippets/stableSort.md b/snippets/stableSort.md index ebcffd72b..b14535e73 100644 --- a/snippets/stableSort.md +++ b/snippets/stableSort.md @@ -14,40 +14,7 @@ const stableSort = (arr, compare) => ``` ```js -// 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 } -]; - -// sort by height -stableSort(input, (a, b) => a.height - b.height); - -/* - Items with the same height are still sorted by weight - which means they preserved their relative order. - [ - { 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 arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +const unstable = [...arr].sort(() => 0); // [5, 0, 2, 3, 4, 1, 6, 7, 8, 9, 10] (in Chrome/NodeJS) +``` diff --git a/test/stableSort/stableSort.test.js b/test/stableSort/stableSort.test.js index cd4a66e9f..28dcbb057 100644 --- a/test/stableSort/stableSort.test.js +++ b/test/stableSort/stableSort.test.js @@ -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(); }); \ No newline at end of file