-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
adds R.stableSort and tests #1456
Closed
Closed
Changes from all commits
Commits
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,41 @@ | ||
var _curry2 = require('./internal/_curry2'); | ||
|
||
/** | ||
* Returns a copy of the list, sorted according to the comparator function, which should accept two values at a | ||
* time and return a negative number if the first value is smaller, a positive number if it's larger, and zero | ||
* if they are equal, values of the same key retain their original order. Please note that this is a **copy** of the list. | ||
* It does not modify the original. | ||
* @func | ||
* @memberOf R | ||
* @category List | ||
* @sig (a,a -> Number) -> [a] -> [a] | ||
* @param {Function} comparator A sorting function :: a -> b -> Int | ||
* @param {Array} list The list to sort | ||
* @return {Array} a new array with its elements sorted by the comparator function. | ||
* @example | ||
* | ||
* var diff = function(a, b) { return a - b; }; | ||
* R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7] | ||
*/ | ||
module.exports = _curry2(function stableSort(comparator, list) { | ||
var sorted = []; | ||
var idx; | ||
|
||
idx = 0; | ||
while (idx < list.length) { | ||
sorted.push([list[idx], idx]); | ||
idx += 1; | ||
} | ||
|
||
sorted.sort(function(a, b) { | ||
var order = comparator(a[0], b[0]); | ||
return order === 0 ? a[1] - b[1] : order; | ||
}); | ||
|
||
idx = 0; | ||
while (idx < list.length) { | ||
sorted[idx] = sorted[idx][0]; | ||
idx += 1; | ||
} | ||
return sorted; | ||
}); |
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,39 @@ | ||
var R = require('..'); | ||
var eq = require('./shared/eq'); | ||
|
||
|
||
describe('stableSort', function() { | ||
it('sorts the elements of a list', function() { | ||
eq(R.sort(function(a, b) {return a - b;}, [3, 1, 8, 1, 2, 5]), [1, 1, 2, 3, 5, 8]); | ||
}); | ||
|
||
it('does not affect the list passed supplied', function() { | ||
var list = [3, 1, 8, 1, 2, 5]; | ||
eq(R.sort(function(a, b) {return a - b;}, list), [1, 1, 2, 3, 5, 8]); | ||
eq(list, [3, 1, 8, 1, 2, 5]); | ||
}); | ||
|
||
it('is curried', function() { | ||
var sortByLength = R.sort(function(a, b) {return a.length - b.length;}); | ||
eq(sortByLength(['one', 'two', 'three', 'four', 'five', 'six']), | ||
['one', 'two', 'six', 'four', 'five', 'three']); | ||
}); | ||
|
||
it('maintains stability', function() { | ||
var list = [ | ||
{type: 'Apple', price: 10}, | ||
{type: 'Pear', price: 100}, | ||
{type: 'Bannana', price: 10}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{type: 'Orange', price: 20} | ||
]; | ||
|
||
eq(R.stableSort(function(a, b) {return a.price - b.price;}, list), | ||
[ | ||
{type: 'Apple', price: 10}, | ||
{type: 'Bannana', price: 10}, | ||
{type: 'Orange', price: 20}, | ||
{type: 'Pear', price: 100} | ||
] | ||
); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove
a -> b -> Int
here. It's confusing that it doesn't match(a,a -> Number)
above, and at any rate it's unnecessary to duplicate the information.