Excel function

SORTBYfunction

Sorts a range by the values in another range.

Excel 2021+
=SORTBY(A2:B9, D2:D9, -1)
Range to drag
A
B
C
D
E
F
G
1
2
3
4
5
6
7
8
9

Drag the corner dots to change array, by_array. By default it returns branch and product (A2:B9) sorted by sales (column D), largest first. Click a cell in the table to make that column the sort range. Try breaking it on purpose to see the errors.

Result

{"Seoul","Milk";"Busan","Coffee";"Daegu","Bread";"Seoul","Apple";"Busan","Apple";"Daegu","Apple";"Busan","Milk";"Seoul","Bread"}

array
A2:B9Drag the corner dots on the table

The range to sort and return.

by_array
D2:D9

One column to sort on. It must have as many rows as the array.

sort_order

1 sorts smallest first (ascending), -1 largest first (descending). Omitted means 1.

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

  1. Example 1. Sort by another column

    =SORTBY(A2:B9, D2:D9, -1)

    Returns branch and product only, ordered by sales (column D), largest first.

  2. 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.

  3. 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

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.