Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Allow ordering for Array inheritors #8944

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
orderByFilter.$inject = ['$parse'];
function orderByFilter($parse){
return function(array, sortPredicate, reverseOrder) {
if (!isArray(array)) return array;
if (!(isArray(array) || array instanceof Array)) return array;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using isArrayLike()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been trying, and it works well, so I've switched to isArrayLike(), since it's a common way for the Angular projects, as far as I understand. Thanks.

if (!sortPredicate) return array;
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate];
sortPredicate = map(sortPredicate, function(predicate){
Expand Down
13 changes: 13 additions & 0 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}]);
Expand Down