|
11 | 11 | * correctly, make sure they are actually being saved as numbers and not strings. |
12 | 12 | * |
13 | 13 | * @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 |
15 | 15 | * used by the comparator to determine the order of elements. |
16 | 16 | * |
17 | 17 | * Can be one of: |
|
24 | 24 | * is interpreted as a property name to be used in comparisons (for example `"special name"` |
25 | 25 | * to sort object by the value of their `special name` property). An expression can be |
26 | 26 | * 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. |
28 | 29 | * - `Array`: An array of function or string predicates. The first predicate in the array |
29 | 30 | * is used for sorting, but when two items are equivalent, the next predicate is used. |
30 | 31 | * |
| 32 | + * If the predicate is missing or empty then it defaults to `'+'`. |
| 33 | + * |
31 | 34 | * @param {boolean=} reverse Reverse the order of the array. |
32 | 35 | * @returns {Array} Sorted copy of the source array. |
33 | 36 | * |
@@ -116,15 +119,21 @@ orderByFilter.$inject = ['$parse']; |
116 | 119 | function orderByFilter($parse){ |
117 | 120 | return function(array, sortPredicate, reverseOrder) { |
118 | 121 | if (!(isArrayLike(array))) return array; |
119 | | - if (!sortPredicate) return array; |
120 | 122 | sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate]; |
| 123 | + if (sortPredicate.length === 0) { sortPredicate = ['+']; } |
121 | 124 | sortPredicate = map(sortPredicate, function(predicate){ |
122 | 125 | var descending = false, get = predicate || identity; |
123 | 126 | if (isString(predicate)) { |
124 | 127 | if ((predicate.charAt(0) == '+' || predicate.charAt(0) == '-')) { |
125 | 128 | descending = predicate.charAt(0) == '-'; |
126 | 129 | predicate = predicate.substring(1); |
127 | 130 | } |
| 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 | + } |
128 | 137 | get = $parse(predicate); |
129 | 138 | if (get.constant) { |
130 | 139 | var key = get(); |
|
0 commit comments