Skip to content

Commit

Permalink
fix($filter): $orderBy filter with dates when sorting against multipl…
Browse files Browse the repository at this point in the history
…e fields

extend compare method to use Date.valueOf() when comparing dates

closes angular#6675
  • Loading branch information
SekibOmazic committed Mar 19, 2014
1 parent 0c65f1a commit 139a6fa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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();
Expand All @@ -115,5 +119,9 @@ function orderByFilter($parse){
return t1 < t2 ? -1 : 1;
}
}

function isDate(obj) {
return Object.prototype.toString.call(obj) === '[object Date]';
}
};
}
30 changes: 30 additions & 0 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 139a6fa

Please sign in to comment.