Skip to content

Commit d532af7

Browse files
committed
Fix orderBy with primitive values by passing true
Introduced by 04a213b
1 parent 1e2815d commit d532af7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/filters/array-filters.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export function orderBy (arr, sortKeys, order) {
8484

8585
if (typeof sortKeys === 'string') {
8686
sortKeys = [sortKeys]
87-
} else if (!sortKeys || !sortKeys.length) {
87+
} else if (!sortKeys || (sortKeys !== true && !sortKeys.length)) {
88+
// we check if sortKeys === true because you can sort primitive values with
89+
// array | orderBy true: http://vuejs.org/api/#orderBy
8890
return arr
8991
}
9092

@@ -101,7 +103,9 @@ export function orderBy (arr, sortKeys, order) {
101103

102104
function recursiveCompare (a, b, i) {
103105
i = i || 0
104-
if (i === sortKeys.length - 1) return compare(a, b, i)
106+
if (sortKeys === true || i === sortKeys.length - 1) {
107+
return compare(a, b, i)
108+
}
105109
return compare(a, b, i) || recursiveCompare(a, b, i + 1)
106110
}
107111

test/unit/specs/filters/filters_spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ describe('Filters', function () {
230230
assertArray(res, [arr[1], arr[2], arr[0]])
231231
})
232232

233+
it('orderBy primitive values', function () {
234+
var filter = filters.orderBy
235+
var arr = [9, 11, 1, 2]
236+
var res = filter(arr, true)
237+
assertArray(res, [arr[2], arr[3], arr[0], arr[1]])
238+
})
239+
233240
it('orderBy multiple fields', function () {
234241
var filter = filters.orderBy
235242
var arr = [

0 commit comments

Comments
 (0)