From dadd1736e03134e437cb3b3b6b17ea74e29c8404 Mon Sep 17 00:00:00 2001 From: marshall007 Date: Thu, 16 Jan 2014 17:52:54 -0600 Subject: [PATCH 1/2] fix(orderBy): support sorting by value --- src/ng/filter/orderBy.js | 3 ++- test/ng/filter/orderBySpec.js | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 961faa368642..c4e8f064e312 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 array.sort(); + if (sortPredicate == '-') return 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}]); From 335c8d79ea7073f0336d00a05a6fe36af43109fe Mon Sep 17 00:00:00 2001 From: marshall007 Date: Thu, 16 Jan 2014 17:58:51 -0600 Subject: [PATCH 2/2] array sorts are in-place, we should copy instead --- src/ng/filter/orderBy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index c4e8f064e312..3a5569c6652c 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -87,8 +87,8 @@ orderByFilter.$inject = ['$parse']; function orderByFilter($parse){ return function(array, sortPredicate, reverseOrder) { if (!isArray(array)) return array; - if (!sortPredicate || sortPredicate == '+') return array.sort(); - if (sortPredicate == '-') return array.sort().reverse(); + 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;