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

Commit 286c0b3

Browse files
committed
feat(orderBy): Stable sort the input
Stable sort the input array
1 parent 32d3cbb commit 286c0b3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/ng/filter/orderBy.js

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ function orderByFilter($parse) {
183183
if (sortPredicate.length === 0) { sortPredicate = ['+']; }
184184

185185
var predicates = processPredicates(sortPredicate, reverseOrder);
186+
predicates.push({ get: function() { return {}; }, descending: reverseOrder ? -1 : 1});
186187

187188
// The next three lines are a version of a Swartzian Transform idiom from Perl
188189
// (sometimes called the Decorate-Sort-Undecorate idiom)

test/ng/filter/orderBySpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,35 @@ describe('Filter: orderBy', function() {
194194
it('should sort mixed array of objects and values in a stable way', function() {
195195
expect(orderBy([{foo: 2}, {foo: {}}, {foo: 3}, {foo: 4}], 'foo')).toEqualData([{foo: 2}, {foo: 3}, {foo: 4}, {foo: {}}]);
196196
});
197+
198+
199+
it('should perform a stable sort', function() {
200+
expect(orderBy([
201+
{foo: 2, bar: 1}, {foo: 1, bar: 2}, {foo: 2, bar: 3},
202+
{foo: 2, bar: 4}, {foo: 1, bar: 5}, {foo: 2, bar: 6},
203+
{foo: 2, bar: 7}, {foo: 1, bar: 8}, {foo: 2, bar: 9},
204+
{foo: 1, bar: 10}, {foo: 2, bar: 11}, {foo: 1, bar: 12}
205+
], 'foo'))
206+
.toEqualData([
207+
{foo: 1, bar: 2}, {foo: 1, bar: 5}, {foo: 1, bar: 8},
208+
{foo: 1, bar: 10}, {foo: 1, bar: 12}, {foo: 2, bar: 1},
209+
{foo: 2, bar: 3}, {foo: 2, bar: 4}, {foo: 2, bar: 6},
210+
{foo: 2, bar: 7}, {foo: 2, bar: 9}, {foo: 2, bar: 11}
211+
]);
212+
213+
expect(orderBy([
214+
{foo: 2, bar: 1}, {foo: 1, bar: 2}, {foo: 2, bar: 3},
215+
{foo: 2, bar: 4}, {foo: 1, bar: 5}, {foo: 2, bar: 6},
216+
{foo: 2, bar: 7}, {foo: 1, bar: 8}, {foo: 2, bar: 9},
217+
{foo: 1, bar: 10}, {foo: 2, bar: 11}, {foo: 1, bar: 12}
218+
], 'foo', true))
219+
.toEqualData([
220+
{foo: 2, bar: 11}, {foo: 2, bar: 9}, {foo: 2, bar: 7},
221+
{foo: 2, bar: 6}, {foo: 2, bar: 4}, {foo: 2, bar: 3},
222+
{foo: 2, bar: 1}, {foo: 1, bar: 12}, {foo: 1, bar: 10},
223+
{foo: 1, bar: 8}, {foo: 1, bar: 5}, {foo: 1, bar: 2}
224+
]);
225+
});
197226
});
198227

199228

0 commit comments

Comments
 (0)