fix(orderBy): allow arrayLike objects to be ordered

Closes #8944
This commit is contained in:
Vitali Tsevan
2014-09-05 12:48:49 +03:00
committed by Peter Bacon Darwin
parent c12e8d4665
commit 94b0f2d35d
2 changed files with 14 additions and 1 deletions

View File

@@ -115,7 +115,7 @@
orderByFilter.$inject = ['$parse'];
function orderByFilter($parse){
return function(array, sortPredicate, reverseOrder) {
if (!isArray(array)) return array;
if (!(isArrayLike(array))) return array;
if (!sortPredicate) return array;
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate];
sortPredicate = map(sortPredicate, function(predicate){

View File

@@ -17,6 +17,19 @@ describe('Filter: orderBy', function() {
expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]);
});
it('should sort inherited from array', function(){
function BaseCollection(){}
BaseCollection.prototype = Array.prototype;
var child = new BaseCollection();
child.push({a:2});
child.push({a:15});
expect(Array.isArray(child)).toBe(false);
expect(child instanceof Array).toBe(true);
expect(orderBy(child, 'a', true)).toEqualData([{a:15}, {a:2}]);
});
it('should sort array by predicate', function() {
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);