Sort and Filter Together With One Formula
By ExcelReady TeamUpdated 7 min read
The short answer
To sort and filter with one formula, put FILTER inside SORT: =SORT(FILTER(A2:D8,B2:B8="North"),4,-1). FILTER keeps the rows that match, and SORT puts them in order by the 4th column, largest first. The result spills into the cells below and updates when the data changes. It needs Excel 2021 or Microsoft 365.
Key takeaways
- Nest FILTER inside SORT: =SORT(FILTER(data, condition), column, order).
- The sort column number counts columns in the filtered result, not the sheet.
- Use -1 for largest to smallest and 1 (or leave it out) for smallest to largest.
- Multiply conditions for AND, add them for OR, and use {2,4},{1,-1} to sort by two columns.
- SORT and FILTER need Excel 2021 or Microsoft 365; older versions show #NAME?.
On this page+
A sort and filter formula in Excel puts FILTER inside SORT, so one cell returns only the rows you want, already in order. =SORT(FILTER(A2:D8,B2:B8="North"),4,-1) lists the North sales from largest to smallest, and it updates by itself when the data changes. This guide is for anyone who builds reports or dashboards in Excel 2021 or Microsoft 365 and is tired of clicking the filter and sort buttons again every week.
The sort and filter formula pattern#
The pattern has two layers. FILTER chooses the rows, and SORT puts them in order:
=SORT(FILTER(array, include, [if_empty]), [sort_index], [sort_order], [by_col])| Part | What it does | Example |
|---|---|---|
| array | The data to return, without headers | A2:D8 |
| include | A TRUE/FALSE test for each row | B2:B8="North" |
| if_empty | What to show if nothing matches (optional) | "No matches" |
| sort_index | Which column of the result to sort by | 4 |
| sort_order | 1 for smallest first, -1 for largest first | -1 |
Both functions are dynamic array functions: the result spills, which means it fills as many cells as it needs, starting from the cell with the formula. They need Excel 2021 or Microsoft 365. The FILTER function guide and the SORT function guide cover each argument in detail.
All the examples use this sales list in A1:D8:
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Rep | Region | Product | Sales |
| 2 | Ana | North | Laptop | 4,200 |
| 3 | Ben | South | Monitor | 1,800 |
| 4 | Chen | North | Monitor | 2,600 |
| 5 | Dev | East | Laptop | 5,100 |
| 6 | Ella | North | Laptop | 3,900 |
| 7 | Femi | South | Laptop | 4,700 |
| 8 | Gus | North | Monitor | 1,500 |
Filter one region and sort by sales#
Click F2 and type:
=SORT(FILTER(A2:D8,B2:B8="North"),4,-1)FILTER keeps the four North rows. SORT then orders them by the 4th column, Sales, with -1 for largest first. The result fills F2:I5:
| Rep | Region | Product | Sales |
|---|---|---|---|
| Ana | North | Laptop | 4,200 |
| Ella | North | Laptop | 3,900 |
| Chen | North | Monitor | 2,600 |
| Gus | North | Monitor | 1,500 |
The headers are not part of the result. Type or copy them into row 1 above the formula. If you change Chen's sales to 5,000, he moves to the top straight away. For more on the inner part, see the complete guide to the FILTER function.
Why FILTER goes inside SORT#
You could also write SORT inside FILTER, but filtering first is usually better. There are two reasons:
- SORT has less work to do. It only orders the rows that passed the filter.
- The column number is simpler.
sort_indexcounts the columns of whatever SORT receives. Here FILTER returns all four columns, so Sales is column 4. If FILTER returned only columns B to D, Sales would be column 3.
That second point is the most common mistake. The number is a position in the result, not a column letter on the sheet.
Add more conditions: AND and OR#
FILTER handles several conditions with simple maths on the TRUE/FALSE tests. Put each test in brackets.
AND (both must be true): multiply. North laptops, largest first:
=SORT(FILTER(A2:D8,(B2:B8="North")*(C2:C8="Laptop")),4,-1)The result is Ana (4,200) then Ella (3,900).
OR (either can be true): add. North or South, largest first:
=SORT(FILTER(A2:D8,(B2:B8="North")+(B2:B8="South")),4,-1)This returns six rows, starting with Femi (4,700) and Ana (4,200). The guide to FILTER with multiple criteria covers longer combinations, and you can also filter by part of a word, as shown in FILTER rows that contain text.
To make the formula easy to reuse, point the condition at a cell. If G1 holds a region name, B2:B8=G1 filters by whatever you type there.
Sort by two columns#
Give SORT a list of columns and a list of orders, in curly brackets. This takes the laptop rows, then sorts by Region A to Z, then by Sales largest first within each region:
=SORT(FILTER(A2:D8,C2:C8="Laptop"),{2,4},{1,-1})| Rep | Region | Product | Sales |
|---|---|---|---|
| Dev | East | Laptop | 5,100 |
| Ana | North | Laptop | 4,200 |
| Ella | North | Laptop | 3,900 |
| Femi | South | Laptop | 4,700 |
Ana comes before Ella because they are both North, and her sales are higher.
To sort by a column you don't want to show, use SORTBY instead of SORT. SORTBY sorts by a separate range of the same height. In Microsoft 365, you can also trim the result to certain columns with CHOOSECOLS: =CHOOSECOLS(SORT(FILTER(A2:D8,B2:B8="North"),4,-1),1,4) returns only Rep and Sales.
Return the top N matching rows#
A sorted, filtered list makes a natural top N report. In Microsoft 365, wrap it in TAKE to keep only the first rows:
=TAKE(SORT(FILTER(A2:D8,C2:C8="Laptop"),4,-1),2)This returns the top two laptop sales: Dev (5,100) and Femi (4,700). TAKE is newer than SORT and FILTER, so it isn't in Excel 2021. There, use INDEX(...,SEQUENCE(2),SEQUENCE(1,4)) around the same formula to pick the first two rows.
To add up the filtered values instead of listing them, see how to filter and sum with one formula. To keep rows within a date range, see filtering by date.
Common errors and how to fix them#
Most problems with a sort and filter formula come from five causes:
| Error | Cause | Fix |
|---|---|---|
| #CALC! | No rows match the condition | Add if_empty: FILTER(A2:D8,B2:B8="West","No matches") |
| #SPILL! | Cells where the result needs to go are not empty | Clear the cells below and to the right |
| #VALUE! | The include range is a different height from the data, or sort_index is bigger than the number of columns | Make both ranges cover rows 2 to 8, and count the result's columns |
| #NAME? | Excel 2019 or older, or a typo in the function name | Use Excel 2021 or Microsoft 365, or the Sort and Filter buttons |
| Wrong order | sort_index points at the wrong column | Count columns in the FILTER result, not on the sheet |
If you add new rows below the data, a fixed range like A2:D8 won't include them. Turn the data into an Excel table with Ctrl+T (Mac: ⌘T) and use the table's column names, so the ranges grow by themselves.
Practise it#
This formula is a favourite in modern Excel tests because it shows you understand spilled results. Try the exercise on this page first. Then practise SORT in a live spreadsheet: you type the formula and it checks it instantly. Follow it with the FILTER function lesson to build conditions with AND and OR.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
In F2, write one formula that returns the North rows only, sorted by Sales (column 4 of the data) from largest to smallest.
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Sales | ||||||
| 2 | |||||||
| 3 | |||||||
| 4 | |||||||
| 5 | |||||||
| 6 | |||||||
| 7 | |||||||
| 8 | |||||||
| 9 | |||||||
| 10 | |||||||
| 11 |
Tip: Start with FILTER(A2:D8,B2:B8="North"), then wrap it in SORT with 4 for the Sales column and -1 for largest first.
Practise this in a lesson
Frequently asked questions
Can you sort and filter at the same time in Excel?
Yes. In Excel 2021 or Microsoft 365, nest FILTER inside SORT, for example =SORT(FILTER(A2:D8,B2:B8="North"),4,-1). The result is a filtered list that is already sorted and updates by itself.
Should SORT go inside FILTER or FILTER inside SORT?
Usually FILTER goes inside SORT. Filtering first means SORT has fewer rows to order, and the sort column number refers to the columns FILTER returns. Both orders give the same rows.
How do I sort a filtered list by two columns?
Give SORT arrays of column numbers and orders: =SORT(FILTER(A2:D8,C2:C8="Laptop"),{2,4},{1,-1}) sorts by column 2 A to Z, then by column 4 largest first.
Why does SORT FILTER return #CALC!?
No rows match the condition, so FILTER has nothing to return. Add the third FILTER argument, for example "No matches", to show a message instead.
Does SORT FILTER work in Excel 2016 or 2019?
No. Both functions need Excel 2021 or Microsoft 365. In older versions, use AutoFilter and the Sort button on the Data tab instead.