What is the SORTBY function?
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], …)SORTBY sorts a range by the values in another range. Where SORT picks the key by column number, SORTBY points straight at the range to sort on, so you can sort by a column that is not in the result.
You can give several keys, so "by branch, then by sales within each branch" takes one formula. Per Microsoft documentation, it is available in Excel 2021 and later and Microsoft 365.
Arguments
- array
- The range to sort and return.
- by_array1
- The range to sort on. It must be one column or one row, and match the size of the array.
- sort_order1
- Optional. 1 for ascending, -1 for descending. Omitted means ascending; any other value gives #VALUE!.
- by_array2, sort_order2, …
- Optional. Used when the earlier keys are equal. Give them in range-and-order pairs.
SORTBY examples
Example 1. Sort by another column
=SORTBY(A2:B9, D2:D9, -1)Returns branch and product only, ordered by sales (column D), largest first.
Example 2. Two keys
=SORTBY(A2:D9, A2:A9, 1, D2:D9, -1)Sorted by branch name, then by sales, largest first, within each branch.
Example 3. Filter, then sort
=SORTBY(FILTER(A2:D9, A2:A9="Seoul"), FILTER(D2:D9, A2:A9="Seoul"), -1)The Seoul rows by sales. Filter the key the same way so the sizes match.
Common mistakes
by_array of a different size
If the array covers rows 2 to 9, so must by_array. Otherwise you get #VALUE!.
Several columns in one by_array
Each by_array must be one column (or one row). For more keys, add more range-and-order pairs.
sort_order of 0 or 2
Only 1 and -1 are allowed. Anything else gives #VALUE!.
The spill range is blocked
If cells the result would spill into already hold values, you get #SPILL!.
FAQ
- How is it different from SORT?
- SORT picks the key by its column number in the array. SORTBY uses a range, so inserting or deleting columns does not shift the key, and you can sort by a column that is not in the result.
- Does it change the original table?
- No. It builds a sorted copy starting at the formula cell.