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
The default sort order is `asc`. You can also specify `desc` or the name of a property from the `customOrders` object. For example, sort parts of the day by the order in which they occur.
43
+
The default value for `options.order` is `'asc'`. You can also specify `'desc'` or the name of a property from the `customOrders` object. For example, sort parts of the day by the order in which they occur.
33
44
34
45
```js
35
46
> sortArray(partsOfTheDay, {
@@ -43,6 +54,28 @@ The default sort order is `asc`. You can also specify `desc` or the name of a pr
43
54
44
55
### Sorting an array of objects
45
56
57
+
#### Sort by object property
58
+
59
+
Pass one or more property names to `options.by` to sort an array of objects by those properties.
Sort by a computed field, i.e. a computed value that doesn't exist in the input dataset. Define your computed fields in the `options.computed` object, each value being a function which takes an array member as input and returns the primitive value to be sorted by. In this example we sort by `total` (the name of the computed field supplied in `options.computed`).
The default sort order is `asc`. You can also specify `desc` or the name of a property from the `customOrders` object. For example, sort parts of the day by the order in which they occur.
43
+
The default value for `options.order` is `'asc'`. You can also specify `'desc'` or the name of a property from the `customOrders` object. For example, sort parts of the day by the order in which they occur.
33
44
34
45
```js
35
46
>sortArray(partsOfTheDay, {
@@ -43,6 +54,28 @@ The default sort order is `asc`. You can also specify `desc` or the name of a pr
43
54
44
55
### Sorting an array of objects
45
56
57
+
#### Sort by object property
58
+
59
+
Pass one or more property names to `options.by` to sort an array of objects by those properties.
Sort by a computed field, i.e. a computed value that doesn't exist in the input dataset. Define your computed fields in the `options.computed` object, each value being a function which takes an array member as input and returns the primitive value to be sorted by. In this example we sort by `total` (the name of the computed field supplied in `options.computed`).
| array | <code>Array</code> | The input array to sort. It is sorted in place. |
155
188
|[options]| <code>object</code> | Sort options. |
156
189
|[options.by]| <code>Array.<string></code> | One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects. |
157
-
|[options.order]| <code>Array.<string></code> | One or more sort orders. Specify `asc`, `desc` or a property name from the `options.customOrders` object. |
190
+
|[options.order]| <code>Array.<string></code> | One or more sort orders. Specify `'asc'`, `'desc'` or a property name from the `options.customOrders` object. |
158
191
|[options.customOrders]| <code>object</code> | A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in. |
159
192
|[options.computed]| <code>object</code> | A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted. |
160
193
|[options.nullRank]| <code>number</code> | Configures whether `null` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`. |
Copy file name to clipboardExpand all lines: dist/index.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -348,9 +348,11 @@
348
348
* @param {Array} array - The input array to sort. It is sorted in place.
349
349
* @param {object} [options] - Sort options.
350
350
* @param {string[]} [options.by] - One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects.
351
-
* @param {string[]} [options.order] - One or more sort orders. Specify `asc`, `desc` or a property name from the `options.customOrders` object.
351
+
* @param {string[]} [options.order] - One or more sort orders. Specify `'asc'`, `'desc'` or a property name from the `options.customOrders` object.
352
352
* @param {object} [options.customOrders] - A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in.
353
353
* @param {object} [options.computed] - A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted.
354
+
* @param {number} [options.nullRank] - Configures whether `null` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
355
+
* @param {number} [options.undefinedRank] - Configures whether `undefined` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
354
356
* @returns {Array} Returns the array that was passed in.
Copy file name to clipboardExpand all lines: dist/index.mjs
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -342,9 +342,11 @@ var t = {
342
342
* @param {Array} array - The input array to sort. It is sorted in place.
343
343
* @param {object} [options] - Sort options.
344
344
* @param {string[]} [options.by] - One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects.
345
-
* @param {string[]} [options.order] - One or more sort orders. Specify `asc`, `desc` or a property name from the `options.customOrders` object.
345
+
* @param {string[]} [options.order] - One or more sort orders. Specify `'asc'`, `'desc'` or a property name from the `options.customOrders` object.
346
346
* @param {object} [options.customOrders] - A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in.
347
347
* @param {object} [options.computed] - A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted.
348
+
* @param {number} [options.nullRank] - Configures whether `null` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
349
+
* @param {number} [options.undefinedRank] - Configures whether `undefined` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
348
350
* @returns {Array} Returns the array that was passed in.
Copy file name to clipboardExpand all lines: index.mjs
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ import t from 'typical/index.mjs'
14
14
* @param {Array} array - The input array to sort. It is sorted in place.
15
15
* @param {object} [options] - Sort options.
16
16
* @param {string[]} [options.by] - One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects.
17
-
* @param {string[]} [options.order] - One or more sort orders. Specify `asc`, `desc` or a property name from the `options.customOrders` object.
17
+
* @param {string[]} [options.order] - One or more sort orders. Specify `'asc'`, `'desc'` or a property name from the `options.customOrders` object.
18
18
* @param {object} [options.customOrders] - A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in.
19
19
* @param {object} [options.computed] - A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted.
20
20
* @param {number} [options.nullRank] - Configures whether `null` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
0 commit comments