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

Commit ac2e631

Browse files
committed
fix(orderBy): Mantain order in array of objects when predicate is not provided
1 parent a9ea309 commit ac2e631

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/ng/filter/orderBy.js

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function orderByFilter($parse){
122122
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate];
123123
if (sortPredicate.length === 0) { sortPredicate = ['+']; }
124124
sortPredicate = sortPredicate.map(function(predicate){
125+
predicate = predicate || '';
125126
var descending = false, get = predicate || identity;
126127
if (isString(predicate)) {
127128
if ((predicate.charAt(0) == '+' || predicate.charAt(0) == '-')) {
@@ -130,6 +131,12 @@ function orderByFilter($parse){
130131
}
131132
if (predicate === '') {
132133
// Effectively no predicate was passed so we compare identity
134+
if (array.some(isObject)) {
135+
return function() {
136+
return descending ? 1 : 0;
137+
};
138+
}
139+
133140
return reverseComparator(function(a, b) {
134141
return compare(a, b);
135142
}, descending);

test/ng/filter/orderBySpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ describe('Filter: orderBy', function() {
9191
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]);
9292
});
9393

94+
it('should maintain order in an array of objects when no predicate is provided', function() {
95+
var array = [{a: 1}, {b: 2}, {c: 3}];
96+
97+
expect(orderBy(array)).toEqualData(array);
98+
expect(orderBy(array, '')).toEqualData(array);
99+
expect(orderBy(array, [])).toEqualData(array);
100+
expect(orderBy(array, [''])).toEqualData(array);
101+
expect(orderBy(array, ['+'])).toEqualData(array);
102+
});
103+
104+
it('should inverse order in an array of objects when minus is provided without predicate', function() {
105+
expect(orderBy([{a: 1}, {b: 2}, {c: 3}], ['-'])).toEqualData([{c: 3}, {b: 2}, {a: 1}]);
106+
});
107+
94108
it('should throw if quoted string predicate is quoted incorrectly', function() {
95109
/*jshint -W008 */
96110
expect(function() {

0 commit comments

Comments
 (0)