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

Commit 607f016

Browse files
fix(orderBy): sort by identity if no predicate is given
Closes #5847 Closes #4579 Closes #9403
1 parent f294244 commit 607f016

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/ng/filter/orderBy.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* correctly, make sure they are actually being saved as numbers and not strings.
1212
*
1313
* @param {Array} array The array to sort.
14-
* @param {function(*)|string|Array.<(function(*)|string)>} expression A predicate to be
14+
* @param {function(*)|string|Array.<(function(*)|string)>=} expression A predicate to be
1515
* used by the comparator to determine the order of elements.
1616
*
1717
* Can be one of:
@@ -24,10 +24,13 @@
2424
* is interpreted as a property name to be used in comparisons (for example `"special name"`
2525
* to sort object by the value of their `special name` property). An expression can be
2626
* optionally prefixed with `+` or `-` to control ascending or descending sort order
27-
* (for example, `+name` or `-name`).
27+
* (for example, `+name` or `-name`). If no property is provided, (e.g. `'+'`) then the array
28+
* element itself is used to compare where sorting.
2829
* - `Array`: An array of function or string predicates. The first predicate in the array
2930
* is used for sorting, but when two items are equivalent, the next predicate is used.
3031
*
32+
* If the predicate is missing or empty then it defaults to `'+'`.
33+
*
3134
* @param {boolean=} reverse Reverse the order of the array.
3235
* @returns {Array} Sorted copy of the source array.
3336
*
@@ -116,15 +119,21 @@ orderByFilter.$inject = ['$parse'];
116119
function orderByFilter($parse){
117120
return function(array, sortPredicate, reverseOrder) {
118121
if (!(isArrayLike(array))) return array;
119-
if (!sortPredicate) return array;
120122
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate];
123+
if (sortPredicate.length === 0) { sortPredicate = ['+']; }
121124
sortPredicate = sortPredicate.map(function(predicate){
122125
var descending = false, get = predicate || identity;
123126
if (isString(predicate)) {
124127
if ((predicate.charAt(0) == '+' || predicate.charAt(0) == '-')) {
125128
descending = predicate.charAt(0) == '-';
126129
predicate = predicate.substring(1);
127130
}
131+
if ( predicate === '' ) {
132+
// Effectively no predicate was passed so we compare identity
133+
return reverseComparator(function(a,b) {
134+
return compare(a, b);
135+
}, descending);
136+
}
128137
get = $parse(predicate);
129138
if (get.constant) {
130139
var key = get();

test/ng/filter/orderBySpec.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ describe('Filter: orderBy', function() {
66
orderBy = $filter('orderBy');
77
}));
88

9-
it('should return same array if predicate is falsy', function() {
10-
var array = [1, 2, 3];
11-
expect(orderBy(array)).toBe(array);
9+
it('should return sorted array if predicate is not provided', function() {
10+
expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
11+
12+
expect(orderBy([2, 1, 3], '')).toEqual([1, 2, 3]);
13+
expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]);
14+
expect(orderBy([2, 1, 3], [''])).toEqual([1, 2, 3]);
15+
16+
expect(orderBy([2, 1, 3], '+')).toEqual([1, 2, 3]);
17+
expect(orderBy([2, 1, 3], ['+'])).toEqual([1, 2, 3]);
18+
19+
expect(orderBy([2, 1, 3], '-')).toEqual([3, 2, 1]);
20+
expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]);
1221
});
1322

1423
it('shouldSortArrayInReverse', function() {

0 commit comments

Comments
 (0)