Dynamic arrays function
SORTBY function in Excel
SORTBY sorts a range by the values in another range, so you can sort names by sales without moving columns.
Checked against Microsoft's and Google's documentation on · Example calculated on a live sheet
- Excel 2016
- Excel 2019
- Excel 2021
- Excel 2024
- Microsoft 365
- Google Sheets
Syntax
SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], …)Arguments in [brackets] are optional. Google Sheets doesn't have this function.
Example
=SORTBY(A2:A9,C2:C9,-1) reads: Return the reps A2:A9 sorted by their July sales C2:C9, highest first.
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Jul | Aug | Sep | Target | By July | |
| 2 | Amara Osei | North | $12,400 | $13,100 | $11,800 | $36,000 | Lina Chen | |
| 3 | Ben Fischer | South | $9,800 | $10,250 | $12,100 | $30,000 | Hugo Martin | |
| 4 | Lina Chen | East | $15,200 | $14,800 | $16,900 | $45,000 | Farid Haddad | |
| 5 | Diego Alvarez | West | $8,700 | $9,100 | $8,400 | $28,000 | Amara Osei | |
| 6 | Eva Novak | North | $11,200 | $12,600 | $13,900 | $36,000 | Eva Novak | |
| 7 | Farid Haddad | South | $13,300 | $12,900 | $14,100 | $39,000 | Grace Kim | |
| 8 | Grace Kim | East | $10,100 | $9,600 | $10,800 | $33,000 | Ben Fischer | |
| 9 | Hugo Martin | West | $14,500 | $15,800 | $15,100 | $42,000 | Diego Alvarez |
H2 shows Lina Chen, the best July, down to Diego Alvarez in H9.
How the spreadsheet calculates it
Sorts A2:A9 by the values in C2:C9, largest first, and spills the result.
- 1. SORTBY: Sorts
A2:A9by the values inC2:C9, largest first, and spills the result.array=`A2:A9`: what to returnby_range=`C2:C9`: what to sort byorder=`-1`: `1` ascending, `-1` descending
More SORTBY examples
=SORTBY(A2:A9,C2:C9,-1)Reps by July sales, highest first.=SORTBY(A2:B9,B2:B9,1,C2:C9,-1)By region, then by July sales within each region.=TAKE(SORTBY(A2:A9,C2:C9,-1),3)The top three (Excel 2024 or Microsoft 365).
Paste any of these into the formula explainer to see it step by step.
Tips
- Make every by_array the same height as array.
- Use a second sort key to break ties in a consistent order.
- Refer to the sorted result with
H2#so other formulas follow it as it grows or shrinks. - Sort by a calculation by giving it as by_array, for example a total of two columns added together.
What SORTBY is used for
Leaderboards and rankings: reps by sales, products by revenue, tasks by due date, without changing the order of the source data.
Arguments
| Argument | Required | What it is |
|---|---|---|
| array | Yes | The range to return, sorted. |
| by_array1 | Yes | The range to sort by, as tall as array. |
| sort_order1 | No | 1 ascending (the default) or -1 descending. |
| by_array2, sort_order2 | No | A second sort key for ties. |
Common mistakes
Using SORTBY in Google Sheets: it doesn't have it. Write
SORT(A2:A9,C2:C9,FALSE).Using it in a file shared with Google Sheets, which doesn't have SORTBY.
A by_array of a different height from array.
In Google Sheets
Google Sheets doesn't have SORTBY. Its SORT does the same job, because its sort column can be a range outside the data: SORT(A2:A9, C2:C9, FALSE).
Errors you may see with SORTBY
Questions
SORT or SORTBY?
In Excel, SORT sorts by a column inside the range (by number); SORTBY sorts by any range. See UNIQUE and SORT.
How do I get a top three?
Wrap it in TAKE: TAKE(SORTBY(A2:A9, C2:C9, -1), 3) in Excel 2024 or Microsoft 365.
Does SORTBY change the original data?
No. It returns a sorted copy that spills into new cells; the source stays as it is.
Can SORTBY sort by a column that isn't in the result?
Yes, that's its main use: SORTBY(A2:A9, C2:C9, -1) returns only the names, sorted by a sales column it doesn't return.
Does SORTBY keep sorting when the data changes?
Yes. It's a formula, so the sorted copy updates whenever the source or the sort column changes.
Sources
Official documentation the facts on this page were checked against.