Shorter stableSort function + use const

This commit is contained in:
simov
2018-02-07 11:17:57 +02:00
parent 276b57a9ca
commit bc6c8633c1
3 changed files with 11 additions and 15 deletions

View File

@ -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. Returns new array without modifying the initial one.
```js ```js
var stableSort = (arr, compare) => const stableSort = (arr, compare) =>
arr arr
.map((item, index) => ({ item, index })) .map((item, index) => ({ item, index }))
.sort((a, b) => .sort((a, b) => compare(a.item, b.item) || a.index - b.index)
((result = compare(a.item, b.item)) => (result !== 0 ? result : a.index - b.index))()
)
.map(({ item }) => item); .map(({ item }) => item);
``` ```
```js ```js
var str = 'abcdefghijklmnopqrstuvwxyz'; const str = 'abcdefghijklmnopqrstuvwxyz';
var compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); 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 // default Array.sort() is unstable in Chrome and NodeJS + modifies the input array
var arr1 = str.split(''); var arr1 = str.split('');
console.log(arr1.sort(compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // false console.log(arr1.sort(compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // false
// stable sort + returns new array // stable sort + returns new array
var arr2 = str.split(''); const arr2 = str.split('');
console.log(stableSort(arr2, compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // true console.log(stableSort(arr2, compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // true
``` ```

View File

@ -1,8 +1,6 @@
var stableSort = (arr, compare) => const stableSort = (arr, compare) =>
arr arr
.map((item, index) => ({ item, index })) .map((item, index) => ({ item, index }))
.sort((a, b) => .sort((a, b) => compare(a.item, b.item) || a.index - b.index)
((result = compare(a.item, b.item)) => (result !== 0 ? result : a.index - b.index))()
)
.map(({ item }) => item); .map(({ item }) => item);
module.exports = stableSort; module.exports = stableSort;

View File

@ -12,11 +12,11 @@ test('Testing stableSort', (t) => {
// test if js engine's Array#sort implementation is stable // test if js engine's Array#sort implementation is stable
// https://gist.github.com/leeoniya/5816476 // https://gist.github.com/leeoniya/5816476
var str = 'abcdefghijklmnopqrstuvwxyz'; const str = 'abcdefghijklmnopqrstuvwxyz';
var compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3);
var input = str.split(''); const input = str.split('');
var output = stableSort(input, compare); const output = stableSort(input, compare);
t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc'); t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc');
t.notDeepEqual(input, output); t.notDeepEqual(input, output);