SORTBY function
Sorts a range by the values in one or more other ranges, which do not have to be part of the output.
Available in Microsoft 365, Excel 2021 and later, Excel for the web, Excel for Mac 2021+
Excel 2021 and laterNot in Google Sheets
Syntax
SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)| Argument | What it means | |
|---|---|---|
array | Required | The range or array to return in sorted order. |
by_array1 | Required | The range or array to sort by. It must be the same height as array (or the same width when sorting columns). |
sort_order1 | Optional | 1 ascending (default) or -1 descending. |
by_array2, sort_order2, ... | Optional | Further sort keys, applied in order to break ties. |
Returns: A sorted copy of array that spills.
How SORTBY works
SORTBY is the flexible sibling of SORT. Instead of naming a column position inside the output, you hand it the actual range to sort by. That means you can sort a list of names by revenue without showing the revenue column, and you can sort by a calculated array - such as a custom priority order - that does not exist anywhere on the sheet.
Arguments come in pairs: a range to sort by and its direction. The first pair is the main sort, the second breaks ties, and so on. Each by_array must have the same number of rows as the data. Because the key can be a formula, you can sort by text length, by a MATCH position against a custom list (High, Medium, Low), or by a random number from RANDARRAY to shuffle a list.
Examples
1Names ordered by a hidden column
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | ||
| 2 | Ana | North | Laptop | 4 | 3600 | Dev | |
| 3 | Ben | South | Monitor | 10 | 1490 | Ana | |
| 4 | Chloe | North | Monitor | 6 | 894 | Ana | |
| 5 | Ana | East | Laptop | 2 | 1800 | Ben | |
| 6 | Dev | South | Laptop | 5 | 4500 | Chloe | |
| 7 | Ben | East | Mouse | 30 | 360 | Ben | |
| 8 | Chloe | North | Mouse | 12 | 144 | Chloe |
Only the Rep names are returned, but they are ordered by Revenue in column E, highest first.
2Custom order: High, Medium, Low
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Task | Priority | |||
| 2 | Update price list | Low | Call supplier | High | |
| 3 | Fix invoice error | High | Fix invoice error | High | |
| 4 | Order stock | Medium | Order stock | Medium | |
| 5 | Call supplier | High | Archive emails | Low | |
| 6 | Archive emails | Low | Update price list | Low |
MATCH turns each priority into 1, 2 or 3, which sorts High first. The second key (the task name, A-Z) breaks ties within each priority.
3Two keys from the data
| A | B | C | D | E | F | G | H | I | J | K | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | ||||||
| 2 | Ana | North | Laptop | 4 | 3600 | Ana | East | Laptop | 2 | 1800 | |
| 3 | Ben | South | Monitor | 10 | 1490 | Ben | East | Mouse | 30 | 360 | |
| 4 | Chloe | North | Monitor | 6 | 894 | Ana | North | Laptop | 4 | 3600 | |
| 5 | Ana | East | Laptop | 2 | 1800 | Chloe | North | Monitor | 6 | 894 | |
| 6 | Dev | South | Laptop | 5 | 4500 | Chloe | North | Mouse | 12 | 144 | |
| 7 | Ben | East | Mouse | 30 | 360 | Dev | South | Laptop | 5 | 4500 | |
| 8 | Chloe | North | Mouse | 12 | 144 | Ben | South | Monitor | 10 | 1490 |
Region A-Z, then Revenue largest first within each region - the same as SORT with {2,5} but using ranges.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#VALUE! | A by_array is a different size from array, or it is not a single row or column. | Make every sort range the same height as the data. |
#SPILL! | The output area is blocked. | Clear the cells below/right of the formula. |
Tips
- Shuffle a list:
=SORTBY(A2:A20, RANDARRAY(ROWS(A2:A20))). - Sort by text length:
=SORTBY(A2:A20, LEN(A2:A20), -1). - Add a final tie-breaker (such as the name) so the order is always predictable.
Google Sheets
Use SORT with an outside range as the sort column, for example SORT(A2:B10, C2:C10, FALSE).
SORTBY in other languages
In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.
| Language | Function name |
|---|---|
| Danish | SORTER.EFTER |
| Dutch | SORTEREN.OP |
| Finnish | LAJITTELE.ARVOJEN.PERUSTEELLA |
| French | TRIERPAR |
| German | SORTIERENNACH |
| Hungarian | RENDEZÉS.ALAP.SZERINT |
| Italian | DATI.ORDINA.PER |
| Polish | SORTUJ.WEDŁUG |
| Portuguese (Brazil) | CLASSIFICARPOR |
| Portuguese (Portugal) | ORDENARPOR |
| Russian | СОРТПО |
| Spanish | ORDENARPOR |
| Swedish | SORTERAEFTER |
| Turkish | SIRALAÖLÇÜT |
Recipes that use SORTBY
- How to list the top 5 values with names in Excel
- How to pick a random sample from a list in Excel
- How to reverse a list in Excel