From 57e9c619f677d5781e5d2a5ea0d3c21edbaf4bbc Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Fri, 10 Oct 2014 14:07:05 -0700 Subject: [PATCH] orderBy should not reverse an array of objects When no predicate is provided, orderBy will reverse an array of objects. This isn't ideal. It should err on the side of preserving the order. It happens because, for objects, in the compare() function, v1 < v2 is false, so the comparator function returns 1 (effectively moving v2 before v1). --- src/ng/filter/orderBy.js | 4 ++-- test/ng/filter/orderBySpec.js | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 32b438517f97..fa86b971afd8 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -175,9 +175,9 @@ function orderByFilter($parse){ v2 = v2.toLowerCase(); } if (v1 === v2) return 0; - return v1 < v2 ? -1 : 1; + return v1 > v2 ? 1 : -1; } else { - return t1 < t2 ? -1 : 1; + return t1 > t2 ? 1 : -1; } } }; diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index 76159efe61e2..03a3c3519d78 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -20,6 +20,13 @@ describe('Filter: orderBy', function() { expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]); }); + it('should maintain order in an array of objects if no predicate is provided', function() { + expect(orderBy([{a: 1}, {b: 2}, {c: 3}])).toEqualData([{a: 1}, {b: 2}, {c: 3}]); + expect(orderBy([{a: 1}, {b: 2}, {c: 3}], '')).toEqualData([{a: 1}, {b: 2}, {c: 3}]); + expect(orderBy([{a: 1}, {b: 2}, {c: 3}], [])).toEqualData([{a: 1}, {b: 2}, {c: 3}]); + expect(orderBy([{a: 1}, {b: 2}, {c: 3}], [''])).toEqualData([{a: 1}, {b: 2}, {c: 3}]); + }); + it('shouldSortArrayInReverse', function() { expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]); expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]);