From bc6c8633c1c2b37e0c738abb16546f5d6a8dba8d Mon Sep 17 00:00:00 2001 From: simov Date: Wed, 7 Feb 2018 11:17:57 +0200 Subject: [PATCH] Shorter stableSort function + use const --- snippets/stableSort.md | 12 +++++------- test/stableSort/stableSort.js | 6 ++---- test/stableSort/stableSort.test.js | 8 ++++---- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/snippets/stableSort.md b/snippets/stableSort.md index 929b34f09..96b352b08 100644 --- a/snippets/stableSort.md +++ b/snippets/stableSort.md @@ -6,24 +6,22 @@ Use `Array.map()` to pair each element of the input array with its corresponding Returns new array without modifying the initial one. ```js -var stableSort = (arr, compare) => +const stableSort = (arr, compare) => arr .map((item, index) => ({ item, index })) - .sort((a, b) => - ((result = compare(a.item, b.item)) => (result !== 0 ? result : a.index - b.index))() - ) + .sort((a, b) => compare(a.item, b.item) || a.index - b.index) .map(({ item }) => item); ``` ```js -var str = 'abcdefghijklmnopqrstuvwxyz'; -var compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); +const str = 'abcdefghijklmnopqrstuvwxyz'; +const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); // 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 // stable sort + returns new array -var arr2 = str.split(''); +const arr2 = str.split(''); console.log(stableSort(arr2, compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // true ``` diff --git a/test/stableSort/stableSort.js b/test/stableSort/stableSort.js index d5b5db0c8..452d6b726 100644 --- a/test/stableSort/stableSort.js +++ b/test/stableSort/stableSort.js @@ -1,8 +1,6 @@ -var stableSort = (arr, compare) => +const stableSort = (arr, compare) => arr .map((item, index) => ({ item, index })) -.sort((a, b) => -((result = compare(a.item, b.item)) => (result !== 0 ? result : a.index - b.index))() -) +.sort((a, b) => compare(a.item, b.item) || a.index - b.index) .map(({ item }) => item); module.exports = stableSort; \ No newline at end of file diff --git a/test/stableSort/stableSort.test.js b/test/stableSort/stableSort.test.js index 50e1bbe6b..7c41e06f3 100644 --- a/test/stableSort/stableSort.test.js +++ b/test/stableSort/stableSort.test.js @@ -12,11 +12,11 @@ test('Testing stableSort', (t) => { // test if js engine's Array#sort implementation is stable // https://gist.github.com/leeoniya/5816476 - var str = 'abcdefghijklmnopqrstuvwxyz'; - var compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); + const str = 'abcdefghijklmnopqrstuvwxyz'; + const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); - var input = str.split(''); - var output = stableSort(input, compare); + const input = str.split(''); + const output = stableSort(input, compare); t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc'); t.notDeepEqual(input, output);