Excel SORTBY Function
SORTBY returns a dynamically sorted copy of a range or array using values from one or more corresponding ranges.
Use it to order names by scores, products by sales, tasks by due dates, or entire tables by columns that may sit outside the returned range.
SORTBY syntax & arguments
Syntax
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
-
1
array
RequiredThe range or array containing the values to return in sorted order.
-
2
by_array1
RequiredThe corresponding row or column whose values determine the primary sort order.
-
3
sort_order1
OptionalControls the primary sort order:
1— Ascending: Sorts from smallest to largest or A to Z. This is the default.-1— Descending: Sorts from largest to smallest or Z to A.
-
4
by_array2, sort_order2
Optional RepeatableAdditional sort ranges and their orders. Excel uses each pair to break ties left by the earlier sort ranges; omit an order to use ascending order.
Example
=SORTBY(A2:C20, C2:C20, -1)
Sort rows in A2:C20 by the corresponding values in C2:C20 from largest to smallest.
SORTBY caveats
-
Sort ranges must line up
Each
by_arraymust be one row high or one column wide and align with the dimension being sorted. Mismatched sizes return a#VALUE!error. -
The result needs room to spill
SORTBY returns a separate dynamic array and leaves the source data unchanged. A blocked output range causes a
#SPILL!error. -
Later sort ranges only break ties
SORTBY applies sort ranges from left to right. A second
by_arrayonly controls rows whose first sort values are equal. -
Only two sort orders are valid
Use
1for ascending order or-1for descending order. Any other value returns a#VALUE!error, while an omitted order defaults to ascending.
Keeping the sort simple? When the column controlling the order is already part of the returned array and you only need one sort level, SORT is usually the clearer choice. Use SORTBY when the sort range sits elsewhere or when additional sort levels need to break ties.
Intro SORTBY practice problem
Solve the intro problem directly here, or open it on its own page.
Advanced SORTBY practice problems
Use SORTBY alongside other Excel functions in realistic, less-prescriptive challenges.
No advanced SORTBY problems are currently available.