Skip to content

Commit 99650de

Browse files
author
Sebastien Armand - sa250111
committed
feat(filter): orderBy for object collections
Allows to use orderBy filter on collection objects and not only arrays. Closes: angular#6210
1 parent cbcfaa2 commit 99650de

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/ng/filter/orderBy.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,19 @@
6262
*/
6363
orderByFilter.$inject = ['$parse'];
6464
function orderByFilter($parse){
65-
return function(array, sortPredicate, reverseOrder) {
66-
if (!isArray(array)) return array;
67-
if (!sortPredicate) return array;
65+
return function(sortable, sortPredicate, reverseOrder) {
66+
if (!isArray(sortable) && !isObject(sortable)) return sortable;
67+
if (!sortPredicate) return sortable;
68+
var array = sortable;
69+
if(isObject(sortable)) {
70+
array = [];
71+
for (var key in sortable) {
72+
if (sortable.hasOwnProperty(key) && key.charAt(0) != '$') {
73+
array.push(sortable[key]);
74+
}
75+
}
76+
}
77+
6878
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate];
6979
sortPredicate = map(sortPredicate, function(predicate){
7080
var descending = false, get = predicate || identity;

test/ng/filter/orderBySpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ describe('Filter: orderBy', function() {
3131
toEqual([{a:2, b:1},{a:15, b:1}]);
3232
});
3333

34+
it('can sort a hash collection', function() {
35+
expect(orderBy({c: {name: 'c'}, a: {name: 'a'}, b: {name: 'b'}}, 'name')).toEqualData([{name: 'a'}, {name: 'b'}, {name: 'c'}]);
36+
expect(orderBy({c: {name: 'c'}, a: {name: 'a'}, b: {name: 'b'}}, '-name')).toEqualData([{name: 'c'}, {name: 'b'}, {name: 'a'}]);
37+
});
38+
3439
});

0 commit comments

Comments
 (0)