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.

Microsoft 365, Excel 2021, Excel 2024, and Excel for the web. Also in Google Sheets.

Syntax =SORTBY(array, by_array1, [sort_order1], ...)

SORTBY
=SORTBY(A2:A5,B2:B5,1)

Arguments

NameRequiredWhat to pass
arrayRequiredThe rows to return, in sorted order.
by_array1RequiredThe values to sort by. It must have the same number of rows as array. It does not have to be part of array.
sort_order1Optional1 ascending, -1 descending. The default is ascending.
by_array2OptionalAnother 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

=SORTBY(A2:A5,B2:B5,1)D2
Bills
ABCD
1MonthBillBy amount
2Jan143.60Apr
3Feb96.20Feb
4Mar118.45Mar
5Apr88.10Jan
A picture of the sheet, not a live calculator. The bills are the sort key. They are not part of the spill.

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.