Function · Dynamic arrays
Excel SORTBY function
The Excel SORTBY function sorts one range by another. The month names, sorted by the bill amounts, come back as Apr, Feb, Mar, Jan.
Syntax =SORTBY(array, by_array1, [sort_order1], ...)
=SORTBY(A2:A5,B2:B5,1)Arguments
| Name | Required | What to pass |
|---|---|---|
| array | Required | The rows to return, in sorted order. |
| by_array1 | Required | The values to sort by. It must have the same number of rows as array. It does not have to be part of array. |
| sort_order1 | Optional | 1 ascending, -1 descending. The default is ascending. |
| by_array2 | Optional | Another sort key, for ties. Pair each extra key with its own order. |
Examples
Month names in bill order
=SORTBY(A2:A5,B2:B5,1)Result Apr, Feb, Mar, Jan
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Month | Bill | By amount | |
| 2 | Jan | 143.60 | Apr | |
| 3 | Feb | 96.20 | Feb | |
| 4 | Mar | 118.45 | Mar | |
| 5 | Apr | 88.10 | Jan |
SORTBY sorts a list by a key that can live in another column, and it can return only the column you ask for. The month names sorted by the bill amounts are Apr, Feb, Mar, Jan. The amounts themselves are not in the result, because the array argument was only the month column. SORT of A2:B5 would have brought both columns along.
Add another by_array when two rows tie and you need a second key. Each key gets its own 1 or -1. The rows stay paired. A sort key that is one row shorter than the list is #VALUE!. Excel 2019 does not have SORTBY.