You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| recordset | <code>Array.<object></code> | Input array of objects |
24
-
| sortBy | <code>string</code> \| <code>Array.<string></code> | One or more property expressions to sort by, e.g. `'name'` or `'name.first'`. |
25
-
|[customOrder]| <code>object</code> | Custom sort order definitions. An object where each key is the property expression and the value is an array specifying the sort order. Example: <br> `{ importance: [ 'speed', 'strength', 'intelligence' ]}`|
23
+
| recordset | <code>Array</code> | Input array of objects or primitive values. |
24
+
| sortBy | <code>Array.<(string\|function())></code> | One or more property expressions to sort by. Expressions may be strings which refer to properties in the input array; they may be strings which refer to properties in the optional `namedConfigs.namedComputedProps` parameter; or they may be inline functions which dynamically calculate values for each property in the input array. |
25
+
| sortTypes | <code>Array.<(string\|Array.<\*>)></code> | The sort types for each of the sortBy expressions. Values may be 'asc', 'desc', an array of custom values, and strings which refer to properties in the optional `namedConfigs.namedCustomOrders` parameter. |
26
+
|[namedConfigs]| <code>object</code> | Provides a means of reusing computed property functions and custom sort types. |
27
+
|[namedConfigs.namedComputedProps]| <code>object</code> | Key/value pairs, where the keys correspond to strings given in the sortBy property list, and the values are functions which will dynamically calculated values for each property in the input array. |
28
+
|[namedConfigs.namedCustomOrders]| <code>object</code> | Key/value pairs, where the keys correspond to strings given in the sortTypes list, and the values are arrays of custom values which define the sort type. |
26
29
27
30
**Example**
28
31
with this data
@@ -37,9 +40,9 @@ with this data
37
40
]
38
41
```
39
42
40
-
sort by `slot` using the default sort order (alphabetical)
43
+
sort by `slot` using an ascending sort type
41
44
```js
42
-
>sortBy(DJs, 'slot')
45
+
>sortBy(DJs, [ 'slot' ], [ 'asc' ])
43
46
[ { name:'Mike', slot:'afternoon' },
44
47
{ name:'Zane', slot:'evening' },
45
48
{ name:'Chris', slot:'morning' },
@@ -48,10 +51,36 @@ sort by `slot` using the default sort order (alphabetical)
Copy file name to clipboardExpand all lines: index.js
+56-37Lines changed: 56 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/**
2
-
* Sort an array of objects by any property value, at any depth, in any custom order.
2
+
* Sort an array of objects or primitives, by any property value, in any combindation of ascending, descending, custom or calculated order.
3
3
*
4
4
* @module sort-array
5
5
* @typicalname sortBy
@@ -9,12 +9,30 @@
9
9
module.exports=sortBy
10
10
11
11
/**
12
-
* @param {object[]} - Input array of objects
13
-
* @param {string|string[]} - One or more property expressions to sort by, e.g. `'name'` or `'name.first'`.
14
-
* @param [customOrder] {object} - Custom sort order definitions. An object where each key is the property expression and the value is an array specifying the sort order. Example: <br>
0 commit comments