Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(orderBy): support sorting by value #5847

Closed
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ orderByFilter.$inject = ['$parse'];
function orderByFilter($parse){
return function(array, sortPredicate, reverseOrder) {
if (!isArray(array)) return array;
if (!sortPredicate) return array;
if (!sortPredicate || sortPredicate == '+') return angular.copy(array).sort();
if (sortPredicate == '-') return angular.copy(array).sort().reverse();
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate];
sortPredicate = map(sortPredicate, function(predicate){
var descending = false, get = predicate || identity;
Expand Down
8 changes: 5 additions & 3 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ describe('Filter: orderBy', function() {
orderBy = $filter('orderBy');
}));

it('should return same array if predicate is falsy', function() {
var array = [1, 2, 3];
expect(orderBy(array)).toBe(array);
it('should return sorted array if predicate is falsy', function() {
expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
});

it('shouldSortArrayInReverse', function() {
Expand All @@ -18,6 +17,9 @@ describe('Filter: orderBy', function() {
});

it('should sort array by predicate', function() {
expect(orderBy([2, 1, 3], '+')).toEqualData([1, 2, 3]);
expect(orderBy([2, 1, 3], '-')).toEqualData([3, 2, 1]);

expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]);
Expand Down