Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/stableSort.js
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
Copy link
Member

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.

* @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;
});
39 changes: 39 additions & 0 deletions test/stableSort.js
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},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Bannana/Banana/

{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}
]
);
});
});