Skip to content

fix(orderBy): guarantee stable sort #30

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

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
* dummy predicate that returns the item's index as `value`.
* (If you are using a custom comparator, make sure it can handle this predicate as well.)
*
* If a custom comparator still can't distinguish between two items, then they will be sorted based
* on their index using the built-in comparator.
*
* Finally, in an attempt to simplify things, if a predicate returns an object as the extracted
* value for an item, `orderBy` will try to convert that object to a primitive value, before passing
* it to the comparator. The following rules govern the conversion:
Expand Down Expand Up @@ -599,7 +602,7 @@ function orderByFilter($parse) {
}
}

return compare(v1.tieBreaker, v2.tieBreaker) * descending;
return (compare(v1.tieBreaker, v2.tieBreaker) || defaultCompare(v1.tieBreaker, v2.tieBreaker)) * descending;
}
};

Expand Down
13 changes: 13 additions & 0 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,19 @@ describe('Filter: orderBy', function() {

expect(orderBy(items, expr, reverse, comparator)).toEqual(sorted);
});

it('should use the default comparator to break ties on a provided comparator', function() {
// Some list that won't be sorted "naturally", i.e. should sort to ['a', 'B', 'c']
var items = ['c', 'a', 'B'];
var expr = null;
function comparator() {
return 0;
}
var reversed = ['B', 'a', 'c'];

expect(orderBy(items, expr, false, comparator)).toEqual(items);
expect(orderBy(items, expr, true, comparator)).toEqual(reversed);
});
});

describe('(object as `value`)', function() {
Expand Down