Excel functions · Dynamic arrays
SORT function in Excel
SORT returns a sorted copy of a range, by one or more of its own columns, without changing the original data. =SORT(A2:E9, 5, -1) lists the orders from largest to smallest amount, and the list re-sorts itself when the data changes.
Last reviewed
Syntax
=SORT(array, [sort_index], [sort_order], [by_col])- arrayrequired
- The range to sort.
- sort_indexoptional
- Which column of array to sort by (1 is the first). Use an array such as {3,5} for several columns.
- sort_orderoptional
- 1 ascending (the default) or -1 descending. Use an array such as {1,-1} to match several columns.
- by_coloptional
- FALSE sorts rows (the default); TRUE sorts columns left to right.
- 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.
- To sort by a column you don't want to return, use SORTBY.
Which versions have SORT
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 SORT with different arguments: SORT(range, sort_column, is_ascending, [sort_column2, is_ascending2, ...]). Each sort column needs its own TRUE/FALSE, and the column can also be a separate range.
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.Largest orders first
=SORT(A2:E9, 5, -1)Result: Spills 8 rows, from 1005 (1,250) down to 1006 (80)
1005 Ben South Laptop 1250 1001 Ana North Laptop 1200 1004 Cara East Laptop 1150 1003 Ana North Monitor 350 1007 Cara East Monitor 320 1002 Ben South Monitor 300 1008 Ana South Keyboard 90 1006 Dev West Keyboard 80 Column 5 is Amount; -1 sorts it from largest to smallest.
2.A sorted list of unique names
=SORT(UNIQUE(B2:B9))Result: Spills Ana, Ben, Cara, Dev
Ana Ben Cara Dev UNIQUE removes the repeats and SORT puts them in A–Z order.
3.Two sort levels
=SORT(A2:E9, {3,5}, {1,-1})Result: Spills 8 rows: by region A–Z, then by amount, largest first
1004 Cara East Laptop 1150 1007 Cara East Monitor 320 1001 Ana North Laptop 1200 1003 Ana North Monitor 350 1005 Ben South Laptop 1250 1002 Ben South Monitor 300 1008 Ana South Keyboard 90 1006 Dev West Keyboard 80 Region (column 3) ascending first, then Amount (column 5) descending within each region.
Common errors and fixes
| Error | Why it happens | Fix |
|---|---|---|
| #VALUE! | sort_index is larger than the number of columns in array, or less than 1. | Count the columns of array, not of the sheet. |
| #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 SORT 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.
Make a live leaderboard of reps sorted by total sales.
Done. I used SORT 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
SORT questions
- Does SORT change my original data?
- No. SORT returns a sorted copy somewhere else; the source range stays as it is.
- What's the difference between SORT and SORTBY?
- SORT sorts by a column number inside the range you return. SORTBY sorts by any range of the same height, even one you don't return, and lists each sort level as a range and an order.
Skip the formula hunt
Upload a spreadsheet and ask in plain English. Free plan, no credit card.
Start free