Excel Function

Excel SORTBY function

Sort a range by any column — even one not included in the output.

SORTBY sorts one range based on the values in a separate range or array. Unlike SORT (which sorts by a column inside the array being sorted), SORTBY can sort a dataset by a column that isn't even included in the output.

Syntax

SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2, ...])
ArgumentWhat it means
array requiredThe range to sort and return.
by_array1 requiredThe column or array to sort by. Must be the same height as array.
sort_order1 optional1 = ascending (default), -1 = descending.
by_array2, sort_order2 optionalAdditional sort keys. Up to 126 sort key pairs allowed.

Basic example

Sort names in column A by score in column C, highest to lowest — but only return the names, not the scores:

=SORTBY(A2:A20, C2:C20, -1)

The output is just the names, sorted by score. SORT couldn't do this — it can only sort by a column that's part of the returned array.

Multiple sort keys

Sort by Region (column B) ascending, then by Amount (column D) descending within each region:

=SORTBY(A2:D50, B2:B50, 1, D2:D50, -1)

Sort by a calculated array

Sort names by the length of each name (shortest to longest), without a helper column:

=SORTBY(A2:A20, LEN(A2:A20), 1)

The by_array doesn't have to be a range — any array of the same size works, including formula results.

💡 SORT vs SORTBY

Use SORT when the sort key is a column inside the array you're sorting. Use SORTBY when the sort key is a separate column, not included in the output, or when you want to sort by a calculated value like LEN or RAND.

Can SORTBY sort randomly?
Yes — use RANDARRAY as the sort key: =SORTBY(A2:A20, RANDARRAY(19)). Every time the sheet recalculates, the order randomises. Useful for random assignment or quiz randomisation.
What version of Excel supports SORTBY?
Excel 365, Excel 2021, and Excel for the web. Not available in Excel 2019 or earlier.

Practice SORTBY for real

Type it in a live spreadsheet, get instant feedback. No videos, no downloads.

Start practising free →