Skip to content

Commit 055189c

Browse files
fix(orderBy): ensure correct ordering with arrays of objects and no predicate
By refactoring to use a Schwartzian transform, we can ensure that objects with no custom `toString` or `toValue` methods are just ordered using their position in the original collection. Closes angular#11866 Closes angular#11312 Closes angular#4282
1 parent 5fb4e14 commit 055189c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/ng/filter/orderBy.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ function orderByFilter($parse) {
205205
function doComparison(v1, v2) {
206206
var result = 0;
207207
for (var index=0, length = predicates.length; index < length; ++index) {
208-
result = result || compare(v1.predicateValues[index], v2.predicateValues[index]) * predicates[index].descending;
208+
result = compare(v1.predicateValues[index], v2.predicateValues[index]) * predicates[index].descending;
209+
if (result) break;
209210
}
210211
return result;
211212
}
@@ -247,14 +248,17 @@ function orderByFilter($parse) {
247248
}
248249

249250
function objectValue(value, index) {
251+
// If `valueOf` is a valid function use that
250252
if (typeof value.valueOf === 'function') {
251253
value = value.valueOf();
252254
if (isPrimitive(value)) return value;
253255
}
256+
// If `toString` is a valid function and not the one from `Object.prototype` use that
254257
if (hasCustomToString(value)) {
255258
value = value.toString();
256259
if (isPrimitive(value)) return value;
257260
}
261+
// We have a basic object so we use the position of the object in the collection
258262
return index;
259263
}
260264

0 commit comments

Comments
 (0)