Skip to content

Commit 139a6fa

Browse files
committed
fix($filter): $orderBy filter with dates when sorting against multiple fields
extend compare method to use Date.valueOf() when comparing dates closes angular#6675
1 parent 0c65f1a commit 139a6fa

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/ng/filter/orderBy.js

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ function orderByFilter($parse){
105105
var t1 = typeof v1;
106106
var t2 = typeof v2;
107107
if (t1 == t2) {
108+
if (isDate(v1) && isDate(v2)) {
109+
v1 = v1.valueOf();
110+
v2 = v2.valueOf();
111+
}
108112
if (t1 == "string") {
109113
v1 = v1.toLowerCase();
110114
v2 = v2.toLowerCase();
@@ -115,5 +119,9 @@ function orderByFilter($parse){
115119
return t1 < t2 ? -1 : 1;
116120
}
117121
}
122+
123+
function isDate(obj) {
124+
return Object.prototype.toString.call(obj) === '[object Date]';
125+
}
118126
};
119127
}

test/ng/filter/orderBySpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ describe('Filter: orderBy', function() {
2323
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]);
2424
});
2525

26+
27+
it('should sort array by date predicate', function() {
28+
// same dates
29+
expect(orderBy([
30+
{ a:new Date('01/01/2014'), b:1 },
31+
{ a:new Date('01/01/2014'), b:3 },
32+
{ a:new Date('01/01/2014'), b:4 },
33+
{ a:new Date('01/01/2014'), b:2 }],
34+
['a', 'b']))
35+
.toEqualData([
36+
{ a:new Date('01/01/2014'), b:1 },
37+
{ a:new Date('01/01/2014'), b:2 },
38+
{ a:new Date('01/01/2014'), b:3 },
39+
{ a:new Date('01/01/2014'), b:4 }]);
40+
41+
// one different date
42+
expect(orderBy([
43+
{ a:new Date('01/01/2014'), b:1 },
44+
{ a:new Date('01/01/2014'), b:3 },
45+
{ a:new Date('01/01/2013'), b:4 },
46+
{ a:new Date('01/01/2014'), b:2 }],
47+
['a', 'b']))
48+
.toEqualData([
49+
{ a:new Date('01/01/2013'), b:4 },
50+
{ a:new Date('01/01/2014'), b:1 },
51+
{ a:new Date('01/01/2014'), b:2 },
52+
{ a:new Date('01/01/2014'), b:3 }]);
53+
});
54+
55+
2656
it('should use function', function() {
2757
expect(
2858
orderBy(

0 commit comments

Comments
 (0)