-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE][ADD] Add stableSort function #592
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
276b57a
Add stableSort function
simov bc6c863
Shorter stableSort function + use const
simov b82e4c5
Update test and snippet description with easier to understand examples
simov 097ab6f
Additional tag for stable sort + shorter example
simov ee64565
Shorter stableSort example and test
simov 7872590
Update stableSort.md
Chalarangelo 7378c9c
Update stableSort.test.js
Chalarangelo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
### stableSort | ||
|
||
Performs stable sort. Useful in Chrome and NodeJS. | ||
|
||
Use `Array.map()` to pair each element of the input array with its corresponding index. Then use `Array.sort()` and a user provided `compare()` function. If the items are equal, sort them by their initial index in the input array. Lastly use `Array.map()` to convert back to the initial array items. | ||
Returns new array without modifying the initial one. | ||
|
||
```js | ||
var 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))() | ||
) | ||
.map(({ item }) => item); | ||
``` | ||
|
||
```js | ||
var str = 'abcdefghijklmnopqrstuvwxyz'; | ||
var 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(''); | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
console.log(arr1.sort(compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // false | ||
|
||
// stable sort + returns new array | ||
var arr2 = str.split(''); | ||
console.log(stableSort(arr2, compare).join('') === 'xyzvwtursopqmnklhijfgdeabc'); // true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var 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))() | ||
) | ||
.map(({ item }) => item); | ||
module.exports = stableSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const test = require('tape'); | ||
const stableSort = require('./stableSort.js'); | ||
|
||
test('Testing stableSort', (t) => { | ||
//For more information on all the methods supported by tape | ||
//Please go to https://github.com/substack/tape | ||
t.true(typeof stableSort === 'function', 'stableSort is a Function'); | ||
//t.deepEqual(stableSort(args..), 'Expected'); | ||
//t.equal(stableSort(args..), 'Expected'); | ||
//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 | ||
var str = 'abcdefghijklmnopqrstuvwxyz'; | ||
var compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3); | ||
|
||
var input = str.split(''); | ||
var output = stableSort(input, compare); | ||
|
||
t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc'); | ||
t.notDeepEqual(input, output); | ||
|
||
t.end(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.