Skip to content

Commit 964e2da

Browse files
committed
fix(orderBy): maintain order in array of objects when predicate is not provided
In ES262, there are two properties which are used to get a primitive value from an Object: - valueOf() -- a method which returns a primitive value represented by the Object - toString() -- a method which returns a string value representing the Object. When comparing objects using relational operators, the abstract operation ToPrimitive(O, TypeHint) is used, which will use these methods to retrieve a value. This CL emulates the behaviour of ToPrimitive(), and ensures that no ordering occurs if the retrieved value is identical. This behaviour was previously used for Date objects, however it can be safely made generic as it applies to all objects. Closes angular#9566 Closes angular#9747
1 parent 32806ca commit 964e2da

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/ng/filter/orderBy.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,17 @@ function orderByFilter($parse) {
163163
function compare(v1, v2) {
164164
var t1 = typeof v1;
165165
var t2 = typeof v2;
166-
if (t1 == t2) {
167-
if (isDate(v1) && isDate(v2)) {
168-
v1 = v1.valueOf();
169-
v2 = v2.valueOf();
166+
if (t1 === t2 && t1 === "object") {
167+
t1 = typeof (v1 = v1.valueOf());
168+
t2 = typeof (v2 = v2.valueOf());
169+
if (t1 === t2 && t1 === "object") {
170+
t1 = typeof (v1 = v1.toString());
171+
t2 = typeof (v2 = v2.toString());
172+
if (t1 === t2 && v1 === v2) return 0;
170173
}
171-
if (t1 == "string") {
174+
}
175+
if (t1 === t2) {
176+
if (t1 === "string") {
172177
v1 = v1.toLowerCase();
173178
v2 = v2.toLowerCase();
174179
}

test/ng/filter/orderBySpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ describe('Filter: orderBy', function() {
104104
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
105105
}).toThrow();
106106
});
107+
108+
109+
it('should not reverse array of objects with no predicate', function() {
110+
var array = [
111+
{ id: 2 },
112+
{ id: 1 },
113+
{ id: 4 },
114+
{ id: 3 }
115+
];
116+
expect(orderBy(array)).toEqualData(array);
117+
});
107118
});
108119

109120

@@ -210,5 +221,16 @@ describe('Filter: orderBy', function() {
210221
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
211222
}).toThrow();
212223
});
224+
225+
226+
it('should not reverse array of objects with no predicate', function() {
227+
var array = [
228+
{ id: 2 },
229+
{ id: 1 },
230+
{ id: 4 },
231+
{ id: 3 }
232+
];
233+
expect(orderBy(array)).toEqualData(array);
234+
});
213235
});
214236
});

0 commit comments

Comments
 (0)