From 32cdaef5f1977faea6fe6e15928a0036b010009f Mon Sep 17 00:00:00 2001 From: Sekib Omazic Date: Wed, 19 Mar 2014 12:30:23 +0100 Subject: [PATCH] fix(orderBy): correctly order by date values Closes #6675 Closes #6746 --- src/ng/filter/orderBy.js | 4 ++++ test/ng/filter/orderBySpec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index abd9f5857e8b..54d71b0f2195 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -153,6 +153,10 @@ function orderByFilter($parse){ var t1 = typeof v1; var t2 = typeof v2; if (t1 == t2) { + if (isDate(v1) && isDate(v2)) { + v1 = v1.valueOf(); + v2 = v2.valueOf(); + } if (t1 == "string") { v1 = v1.toLowerCase(); v2 = v2.toLowerCase(); diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index f7003d4b76e4..c97ea43700b1 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -23,6 +23,36 @@ describe('Filter: orderBy', function() { expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]); }); + + it('should sort array by date predicate', function() { + // same dates + expect(orderBy([ + { a:new Date('01/01/2014'), b:1 }, + { a:new Date('01/01/2014'), b:3 }, + { a:new Date('01/01/2014'), b:4 }, + { a:new Date('01/01/2014'), b:2 }], + ['a', 'b'])) + .toEqualData([ + { a:new Date('01/01/2014'), b:1 }, + { a:new Date('01/01/2014'), b:2 }, + { a:new Date('01/01/2014'), b:3 }, + { a:new Date('01/01/2014'), b:4 }]); + + // one different date + expect(orderBy([ + { a:new Date('01/01/2014'), b:1 }, + { a:new Date('01/01/2014'), b:3 }, + { a:new Date('01/01/2013'), b:4 }, + { a:new Date('01/01/2014'), b:2 }], + ['a', 'b'])) + .toEqualData([ + { a:new Date('01/01/2013'), b:4 }, + { a:new Date('01/01/2014'), b:1 }, + { a:new Date('01/01/2014'), b:2 }, + { a:new Date('01/01/2014'), b:3 }]); + }); + + it('should use function', function() { expect( orderBy(