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.
SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2, ...])| Argument | What it means |
|---|---|
| array required | The range to sort and return. |
| by_array1 required | The column or array to sort by. Must be the same height as array. |
| sort_order1 optional | 1 = ascending (default), -1 = descending. |
| by_array2, sort_order2 optional | Additional sort keys. Up to 126 sort key pairs allowed. |
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.
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 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.
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.
=SORTBY(A2:A20, RANDARRAY(19)). Every time the sheet recalculates, the order randomises. Useful for random assignment or quiz randomisation.Type it in a live spreadsheet, get instant feedback. No videos, no downloads.
Start practising free →