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

fix(orderBy): maintain order in array of objects when predicate is not provided #10311

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 10 additions & 5 deletions src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,17 @@ function orderByFilter($parse) {
function compare(v1, v2) {
var t1 = typeof v1;
var t2 = typeof v2;
if (t1 == t2) {
if (isDate(v1) && isDate(v2)) {
v1 = v1.valueOf();
v2 = v2.valueOf();
if (t1 === t2 && t1 === "object") {
t1 = typeof (v1.valueOf ? v1 = v1.valueOf() : v1);
t2 = typeof (v2.valueOf ? v2 = v2.valueOf() : v2);
if (t1 === t2 && t1 === "object") {
t1 = typeof (v1.toString ? v1 = v1.toString() : v1);
t2 = typeof (v2.toString ? v2 = v2.toString() : v2);
if (t1 === t2 && v1 === v2 || t1 === "object") return 0;
}
if (t1 == "string") {
}
if (t1 === t2) {
if (t1 === "string") {
v1 = v1.toLowerCase();
v2 = v2.toLowerCase();
}
Expand Down
42 changes: 42 additions & 0 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ describe('Filter: orderBy', function() {
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
}).toThrow();
});


it('should not reverse array of objects with no predicate', function() {
var array = [
{ id: 2 },
{ id: 1 },
{ id: 4 },
{ id: 3 }
];
expect(orderBy(array)).toEqualData(array);
});


it('should not reverse array of objects with null prototype and no predicate', function() {
var array = [2,1,4,3].map(function(id) {
var obj = Object.create(null);
obj.id = id;
return obj;
});
expect(orderBy(array)).toEqualData(array);
});
});


Expand Down Expand Up @@ -210,5 +231,26 @@ describe('Filter: orderBy', function() {
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
}).toThrow();
});


it('should not reverse array of objects with no predicate', function() {
var array = [
{ id: 2 },
{ id: 1 },
{ id: 4 },
{ id: 3 }
];
expect(orderBy(array)).toEqualData(array);
});


it('should not reverse array of objects with null prototype and no predicate', function() {
var array = [2,1,4,3].map(function(id) {
var obj = Object.create(null);
obj.id = id;
return obj;
});
expect(orderBy(array)).toEqualData(array);
});
});
});