Excel functions · Dynamic arrays
SORTBY function in Excel
SORTBY sorts a range by the values in one or more other ranges, which don't have to be part of the result, such as a list of reps sorted by their order amounts. Each sort level is a range and an order, so multi-level and custom-order sorts are easy to read.
Last reviewed
Syntax
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)- arrayrequired
- The range to sort and return.
- by_array1required
- The range to sort by. Same height as array (or same width when sorting columns).
- sort_order1optional
- 1 ascending (the default) or -1 descending.
- by_array2, sort_order2, ...optional
- More sort levels, applied in order to break ties.
- The result spills: type the formula in one cell and it fills as many cells as it needs, and it updates when the data changes.
- Rows that tie on every sort level keep their original order.
Which versions have SORTBY
Microsoft 365, Excel for the web, and Excel 2021 or later (Windows and Mac). Not in Excel 2019 or 2016.
| Version | Has it |
|---|---|
| Microsoft 365 (Windows and Mac) | Yes |
| Excel for the web | Yes |
| Excel 2024 | Yes |
| Excel 2021 | Yes |
| Excel 2019 | No |
| Excel 2016 | No |
Google Sheets
Google Sheets has no SORTBY. Its SORT accepts a separate range as the sort column, which does the same job: =SORT(B2:B9, E2:E9, FALSE).
Examples
The examples use this sample sheet.
| Row | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | Order ID | Rep | Region | Product | Amount |
| 2 | 1001 | Ana | North | Laptop | 1200 |
| 3 | 1002 | Ben | South | Monitor | 300 |
| 4 | 1003 | Ana | North | Monitor | 350 |
| 5 | 1004 | Cara | East | Laptop | 1150 |
| 6 | 1005 | Ben | South | Laptop | 1250 |
| 7 | 1006 | Dev | West | Keyboard | 80 |
| 8 | 1007 | Cara | East | Monitor | 320 |
| 9 | 1008 | Ana | South | Keyboard | 90 |
1.Sort names by a column you don't return
=SORTBY(B2:B9, E2:E9, -1)Result: Spills Ben, Ana, Cara, Ana, Cara, Ben, Ana, Dev
Ben Ana Cara Ana Cara Ben Ana Dev The reps, ordered by the amount of each order from 1,250 down to 80.
2.Two sort levels
=SORTBY(A2:A9, C2:C9, 1, E2:E9, -1)Result: Spills 1004, 1007, 1001, 1003, 1005, 1002, 1008, 1006
1004 1007 1001 1003 1005 1002 1008 1006 Order IDs by region A–Z, then by amount from largest to smallest within each region.
3.A custom order
=SORTBY(A2:A9, XMATCH(C2:C9, {"North","South","East","West"}))Result: Spills 1001, 1003, 1002, 1005, 1008, 1004, 1007, 1006
1001 1003 1002 1005 1008 1004 1007 1006 XMATCH turns each region into its place in your list (North 1, South 2…), and SORTBY sorts by that number.
Common errors and fixes
| Error | Why it happens | Fix |
|---|---|---|
| #VALUE! | A by_array isn't the same height as array, or a sort order isn't 1 or -1. | Use ranges that cover the same rows, and 1 or -1 for each order. |
| #SPILL! | The result needs more cells than are free: something is in the way, or the formula is inside an Excel table. | Clear the cells below and to the right, or move the formula outside the table. |
| #NAME? | Your Excel doesn't have dynamic array functions (Excel 2019 or older). | Open the file in Microsoft 365, Excel for the web, or Excel 2021 or later. |
Do it with GPTExcel
Skip writing SORTBY by hand
Upload your Excel or CSV file to Chat and ask in plain English. GPTExcel writes the formula for your file and your version of Excel or Google Sheets.
Sort my regions in the order North, South, East, West instead of A to Z.
Done. I used SORTBY on your sheet and checked the results against your data.
- Written for your platform and version
- Checked against your actual data
- Newest functions looked up in official docs
SORTBY questions
- Does Google Sheets have SORTBY?
- No. In Google Sheets, use SORT with a separate range as the sort column, such as =SORT(B2:B9, E2:E9, FALSE), which sorts the reps by amount, largest first.
- How do I sort by a custom list in Excel with a formula?
- Turn each value into its position in your list with XMATCH and sort by that: =SORTBY(A2:A9, XMATCH(C2:C9, {"North","South","East","West"})).
Skip the formula hunt
Upload a spreadsheet and ask in plain English. Free plan, no credit card.
Start free