@@ -72,48 +72,60 @@ export function filterBy (arr, search, delimiter) {
72
72
/**
73
73
* Filter filter for arrays
74
74
*
75
- * @param {String|Array<String>|Function } sortKeys
75
+ * @param {String|Array<String>|Function } ... sortKeys
76
76
* @param {Number } [order]
77
77
*/
78
78
79
- export function orderBy ( arr , sortKeys , order ) {
79
+ export function orderBy ( arr ) {
80
+ let comparator = null
81
+ let sortKeys
80
82
arr = convertArray ( arr )
81
- order = ( order && order < 0 ) ? - 1 : 1
82
- let recursiveCompare = null
83
83
84
- if ( typeof sortKeys === 'string' ) {
85
- sortKeys = [ sortKeys ]
86
- } else if ( typeof sortKeys === 'function' ) {
87
- recursiveCompare = function ( a , b ) {
88
- return sortKeys ( a , b ) * order
89
- }
90
- } else if ( ! sortKeys || ( sortKeys !== true && ! sortKeys . length ) ) {
91
- // we check if sortKeys === true because you can sort primitive values with
92
- // array | orderBy true: http://vuejs.org/api/#orderBy
93
- return arr
84
+ // determine order (last argument)
85
+ let args = toArray ( arguments , 1 )
86
+ let order = args [ args . length - 1 ]
87
+ if ( typeof order === 'number' ) {
88
+ order = order < 0 ? - 1 : 1
89
+ args = args . length > 1 ? args . slice ( 0 , - 1 ) : args
90
+ } else {
91
+ order = 1
94
92
}
95
93
96
- function compare ( a , b , sortKeyIndex ) {
97
- const sortKey = sortKeys [ sortKeyIndex ]
98
- if ( sortKey !== '$key' ) {
99
- if ( isObject ( a ) && '$value' in a ) a = a . $value
100
- if ( isObject ( b ) && '$value' in b ) b = b . $value
94
+ // determine sortKeys & comparator
95
+ let firstArg = args [ 0 ]
96
+ if ( ! firstArg ) {
97
+ return arr
98
+ } else if ( typeof firstArg === 'function' ) {
99
+ // custom comparator
100
+ comparator = function ( a , b ) {
101
+ return firstArg ( a , b ) * order
102
+ }
103
+ } else {
104
+ // string keys. flatten first
105
+ sortKeys = Array . prototype . concat . apply ( [ ] , args )
106
+ comparator = function ( a , b , i ) {
107
+ i = i || 0
108
+ return i >= sortKeys . length - 1
109
+ ? baseCompare ( a , b , i )
110
+ : baseCompare ( a , b , i ) || comparator ( a , b , i + 1 )
101
111
}
102
- a = isObject ( a ) ? getPath ( a , sortKey ) : a
103
- b = isObject ( b ) ? getPath ( b , sortKey ) : b
104
- return a === b ? 0 : a > b ? order : - order
105
112
}
106
113
107
- recursiveCompare = recursiveCompare || function ( a , b , i ) {
108
- i = i || 0
109
- if ( sortKeys === true || i === sortKeys . length - 1 ) {
110
- return compare ( a , b , i )
114
+ function baseCompare ( a , b , sortKeyIndex ) {
115
+ const sortKey = sortKeys [ sortKeyIndex ]
116
+ if ( sortKey ) {
117
+ if ( sortKey !== '$key' ) {
118
+ if ( isObject ( a ) && '$value' in a ) a = a . $value
119
+ if ( isObject ( b ) && '$value' in b ) b = b . $value
120
+ }
121
+ a = isObject ( a ) ? getPath ( a , sortKey ) : a
122
+ b = isObject ( b ) ? getPath ( b , sortKey ) : b
111
123
}
112
- return compare ( a , b , i ) || recursiveCompare ( a , b , i + 1 )
124
+ return a === b ? 0 : a > b ? order : - order
113
125
}
114
126
115
127
// sort on a copy to avoid mutating original array
116
- return arr . slice ( ) . sort ( recursiveCompare )
128
+ return arr . slice ( ) . sort ( comparator )
117
129
}
118
130
119
131
/**
0 commit comments