Skip to content

Commit cafc829

Browse files
fix(orderBy): ensure correct ordering with arrays of objects and no predicate
Closes angular#11866
1 parent 239de1a commit cafc829

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/ng/filter/orderBy.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function orderByFilter($parse) {
212212
};
213213

214214
function processPredicates(sortPredicate, reverseOrder) {
215-
var reverseOrder = reverseOrder ? -1 : 1;
215+
reverseOrder = reverseOrder ? -1 : 1;
216216
return sortPredicate.map(function(predicate) {
217217
var descending = 1, get = identity;
218218

@@ -246,33 +246,27 @@ function orderByFilter($parse) {
246246
}
247247
}
248248

249-
function objectToString(value) {
249+
function objectValue(value, index) {
250250
if (typeof value.valueOf === 'function') {
251251
value = value.valueOf();
252252
if (isPrimitive(value)) return value;
253253
}
254-
if (typeof value.toString === 'function') {
254+
if (hasCustomToString(value)) {
255255
value = value.toString();
256256
if (isPrimitive(value)) return value;
257257
}
258-
return '';
258+
return index;
259259
}
260260

261261
function getPredicateValue(value, index) {
262262
var type = typeof value;
263263
if (value === null) {
264-
type = 'null';
264+
type = 'string';
265+
value = 'null';
265266
} else if (type === 'string') {
266267
value = value.toLowerCase();
267268
} else if (type === 'object') {
268-
value = objectToString(value);
269-
270-
// If the object is a POJO then it doesn't really have a well defined ordering
271-
// To allow us to reverse the order of these objects, we artificially use its current
272-
// index in the collection as its value
273-
if (value === '[object Object]') {
274-
value = index;
275-
}
269+
value = objectValue(value, index);
276270
}
277271
return { value: value, type: type };
278272
}

test/ng/filter/orderBySpec.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Filter: orderBy', function() {
2323
});
2424

2525

26-
it('shouldSortArrayInReverse', function() {
26+
it('should reverse collection if `reverseOrder` param is truthy', function() {
2727
expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]);
2828
expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]);
2929
expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]);
@@ -178,12 +178,17 @@ describe('Filter: orderBy', function() {
178178
null
179179
];
180180
expect(orderBy(array)).toEqualData([
181-
null,
182-
null,
183181
{ id: 2 },
184-
{ id: 3 }
182+
{ id: 3 },
183+
null,
184+
null
185185
]);
186186
});
187+
188+
189+
it('should sort array of arrays as Array.prototype.sort', function() {
190+
expect(orderBy([ ['one'], ['two'], ['three'] ])).toEqualData([ ['one'], ['three'], ['two'] ]);
191+
});
187192
});
188193

189194

@@ -321,10 +326,10 @@ describe('Filter: orderBy', function() {
321326
null
322327
];
323328
expect(orderBy(array)).toEqualData([
324-
null,
325-
null,
326329
{ id: 2 },
327-
{ id: 3 }
330+
{ id: 3 },
331+
null,
332+
null
328333
]);
329334
});
330335
});

0 commit comments

Comments
 (0)