diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 961faa368642..3a5569c6652c 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -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; diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index 5c1178910255..bd9a8bd4d461 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -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() { @@ -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}]);