Excel guide · 3 min read
How to sort data with a formula in Excel
=SORT(A2:D9,4,-1) to return a copy of the data sorted by its 4th column, largest first. The result spills into the cells below and re-sorts by itself whenever the source changes, and your original data stays untouched. SORT is in Excel 365 and Excel 2021 and later.Sample data
Every result on this page was calculated from this sheet by the same spreadsheet engine the Cellwise app uses.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Rep | Region | Deals | Revenue |
| 2 | Maya Chen | West | 14 | 48200 |
| 3 | Luis Ortega | East | 9 | 61750 |
| 4 | Priya Nair | West | 21 | 39900 |
| 5 | Tom Becker | Central | 11 | 52300 |
| 6 | Aisha Bello | East | 17 | 70400 |
| 7 | Sam Walsh | Central | 6 | 28650 |
| 8 | Grace Kim | West | 12 | 55100 |
| 9 | Owen Hart | East | 8 | 33800 |
SORT by one column
SORT takes the range, the column number to sort by (counted inside the range, not the sheet), and 1 for ascending or -1 for descending. Leave out the header row: you want the labels to stay put above the result.
Type the formula once in an empty cell with enough room below and to the right. Excel fills the rest. That block is a spill range: you cannot type inside it, and it grows or shrinks as rows are added to the source.
Result: spills 8 rows × 4 columns
| Aisha Bello | East | 17 | 70400 |
| Luis Ortega | East | 9 | 61750 |
| Grace Kim | West | 12 | 55100 |
| Tom Becker | Central | 11 | 52300 |
| Maya Chen | West | 14 | 48200 |
| Priya Nair | West | 21 | 39900 |
| Owen Hart | East | 8 | 33800 |
| Sam Walsh | Central | 6 | 28650 |
All 8 reps, 4 columns, from Aisha Bello (70,400) down to Sam Walsh (28,650).
Default sort: first column, A to Z
With only the range, SORT orders by the first column, ascending. Text sorts alphabetically and ignores case. Here that is by first name, because the names are stored first name first.
Result: spills 8 rows × 4 columns
| Aisha Bello | East | 17 | 70400 |
| Grace Kim | West | 12 | 55100 |
| Luis Ortega | East | 9 | 61750 |
| Maya Chen | West | 14 | 48200 |
| Owen Hart | East | 8 | 33800 |
| Priya Nair | West | 21 | 39900 |
| Sam Walsh | Central | 6 | 28650 |
| Tom Becker | Central | 11 | 52300 |
The table in A to Z order of the Rep column, from Aisha Bello to Tom Becker.
Sort a filtered list
Wrap FILTER inside SORT to pick rows and order them in one step. This is the formula version of filtering a table and then sorting it, except it never goes stale.
The column number still refers to the columns of what FILTER returns, which here is the same four columns.
Result: spills 3 rows × 4 columns
| Grace Kim | West | 12 | 55100 |
| Maya Chen | West | 14 | 48200 |
| Priya Nair | West | 21 | 39900 |
3 West reps by revenue: Grace Kim 55,100, Maya Chen 48,200, Priya Nair 39,900.
Sort by two columns, or by a column you do not show
For a second sort key, give SORT arrays of columns and orders: =SORT(A2:D9,{2,4},{1,-1}) sorts by Region A to Z, then by Revenue largest first within each region. That form is valid in Excel 365, but the Cellwise practice engine cannot run it, so try it in Excel.
SORTBY sorts one range by another range, so the sort column does not have to be in the output: =SORTBY(A2:A9,D2:D9,-1) lists just the names, ordered by revenue. SORTBY is also Excel 365 and 2021 and later.
A sorted list of unique values
SORT and UNIQUE together give a clean, alphabetical list of categories that updates as new ones appear. It is the usual source for a dropdown list.
Result: spills 3 rows × 1 column
| Central |
| East |
| West |
3 regions in A to Z order: Central, East, West.
Older Excel, or a one-off sort
In Excel 2019 and earlier there is no SORT. For a ranked list of numbers, =LARGE(D2:D9,1), =LARGE(D2:D9,2) and so on return the values in order, and INDEX with MATCH fetches the matching name. If you only need to sort once, the ribbon is faster and changes the data in place.
- Click any cell in the data.
- Go to Data > Sort.
- Tick My data has headers, choose Revenue under Sort by, and Largest to Smallest under Order.
- Click Add Level for a second key, then OK.
Common mistakes
#SPILL! instead of a sorted list
Something is in the way: a value, even a space, in the cells the result needs. Clear the area below and to the right. SORT also cannot spill inside an Excel table, so put the formula outside it.
Including the header row
SORT(A1:D9) sorts the header in with the data, so "Revenue" lands among the names. Start the range on row 2 and type the headers above the formula.
Counting the sort column from column A
The index is the position inside the range. In SORT(B2:D9,3,-1) the 3 means column D, because B is column 1 of that range.
Numbers stored as text sort in the wrong place
Text sorts after numbers in ascending order, so "9" can land below 61,750. Convert the column to real numbers first.
FAQ
How do I sort largest to smallest with a formula?
Use -1 as the third argument: =SORT(A2:D9,4,-1). Use 1, or leave it out, for smallest to largest.
Does SORT change my original data?
No. SORT returns a sorted copy somewhere else on the sheet. The source keeps its order, and the copy updates whenever the source changes.
Why do I get #NAME? with SORT?
Your Excel version does not have the function. SORT needs Excel 365 or Excel 2021 or later. Use Data > Sort, or LARGE and SMALL with INDEX and MATCH, in older versions.
Can SORT sort horizontally?
Yes. The fourth argument, by_col, set to TRUE sorts columns instead of rows: =SORT(B1:E2,2,-1,TRUE) orders the columns by the values in row 2.
Functions used
SORTSorts a range, spillsFILTERKeeps matching rows, spillsUNIQUERemoves duplicates, spillsLARGEk-th largestINDEXValue at a positionMATCHPosition of a value
Related guides
- How to filter with multiple criteria in Excel
- How to create a unique list in Excel
- How to find the top 5 values in Excel
- #SPILL! error in Excel
- How to count unique values in Excel
- How to return multiple matches in Excel
Updated