Sorting & Filtering
Sorting puts rows in a useful order: highest salary first, oldest invoice first, names A to Z. Filtering hides the rows you do not need right now, so you can see only Jaipur staff or only unpaid bills. Together they answer most everyday questions about a list in seconds.
In this lesson you will sort by one and several columns, sort in a custom order, filter by text, numbers and dates, and total only the visible rows. You will also see how the menu commands compare with the SORT and FILTER functions.
The sample sheet
A travel agency with offices in three cities keeps this staff list:
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Emp ID | Name | Department | City | Joined | Salary (₹) |
| 2 | E101 | Ananya | Sales | Pune | 12-Mar-2022 | 38000 |
| 3 | E102 | Rohan | Accounts | Jaipur | 01-Jul-2020 | 45500 |
| 4 | E103 | Isha | Sales | Jaipur | 15-Jan-2024 | 32000 |
| 5 | E104 | Farhan | Support | Pune | 20-Sep-2021 | 29500 |
| 6 | E105 | Meera | Accounts | Kochi | 05-Feb-2019 | 52000 |
| 7 | E106 | Kabir | Sales | Kochi | 10-Aug-2023 | 34500 |
| 8 | E107 | Priya | Support | Jaipur | 03-Nov-2022 | 31000 |
The data follows two rules that make sorting and filtering safe: one heading row, and no completely empty rows or columns inside the list.
Sorting by one column
Click any single cell in the Salary column, then choose Data > Sort Largest to Smallest (the Z-A button). Excel detects the whole list, keeps the heading row in place, and moves complete rows, so each salary stays with its employee. Meera's row comes first.
For text columns the buttons read A to Z and Z to A; for dates they sort oldest to newest or newest to oldest.
Sorting by several columns
To list staff by department, with the highest salary first inside each department, use Data > Sort (the large Sort button):
- Sort by Department, Order A to Z.
- Click Add Level. Then by Salary (₹), Order Largest to Smallest.
- Make sure My data has headers is ticked, and click OK.
| B | C | F | |
|---|---|---|---|
| 2 | Meera | Accounts | 52000 |
| 3 | Rohan | Accounts | 45500 |
| 4 | Ananya | Sales | 38000 |
| 5 | Kabir | Sales | 34500 |
| 6 | Isha | Sales | 32000 |
| 7 | Priya | Support | 31000 |
| 8 | Farhan | Support | 29500 |
Excel applies the first level, then uses the second level only to order rows that tie on the first.
Sorting in a custom order
Alphabetical order is not always right. Months should go January to December, and a priority column should go High, Medium, Low. In the Sort dialog, open the Order list and choose Custom List. Excel already knows days and months; to add your own, type the items in order (for example Sales, Accounts, Support) and click Add.
Filtering rows
Click inside the list and press Ctrl+Shift+L (or Data > Filter). A drop-down arrow appears on every heading. Click the arrow on City, untick Select All, tick Jaipur and click OK. Only Rohan, Isha and Priya remain visible; the row numbers turn blue and the status bar shows "3 of 7 records found". The other rows are hidden, not deleted.
Each drop-down offers filters that match the column's data type:
| Column type | Useful filters |
|---|---|
| Text | Equals, Begins With, Contains, Does Not Contain |
| Numbers | Greater Than, Between, Top 10, Above Average |
| Dates | Before, After, Between, This Month, Last Year |
Filters on different columns combine with AND: City is Jaipur and Department is Sales leaves only Isha. Inside one column's custom filter dialog you can choose AND or OR between two conditions. To remove all filters, use Data > Clear; to turn the arrows off, press Ctrl+Shift+L again.
Totals of visible rows only
SUM adds every row, hidden or not. While filtering, you usually want the total of what you can see. SUBTOTAL does that. Its first argument picks the calculation:
=SUBTOTAL(109, F2:F8)108500
With the Jaipur filter on, this adds only 45500 + 32000 + 31000. Clear the filter and the same formula shows 262500. Put such totals above the list or in another column, so a filter never hides the total itself.
| First argument | Calculation |
|---|---|
109 | Sum |
101 | Average |
103 | Count of non-empty cells (COUNTA) |
104 / 105 | Max / Min |
The numbers 1 to 11 do the same jobs but also include rows you hid by hand (right-click > Hide); 101 to 111 ignore them. Both ignore rows hidden by a filter. =SUBTOTAL(103, B2:B8) counts the visible names: 3 for Jaipur.
Menu commands or functions?
The Sort and Filter commands change the view of the original data. The SORT and FILTER functions from Dynamic Arrays leave the data alone and build a sorted or filtered copy that updates automatically:
=FILTER(B2:F8, (F2:F8>=35000) * ((D2:D8="Pune") + (D2:D8="Kochi")))This lists staff earning ₹35,000 or more in Pune or Kochi: Ananya and Meera. Use the menu for quick, one-off questions; use the functions for reports that other people will open again and again.
Removing duplicates
Data > Remove Duplicates deletes repeated rows. Tick the columns that define a duplicate, for example only Emp ID. It permanently removes rows, so work on a copy, or use UNIQUE to list distinct values without deleting anything.
Common mistakes
- Sorting one column alone. Rows get scrambled. Always expand the selection, or click one cell and let Excel detect the list.
- Empty rows inside the data. Excel stops detecting the list at the gap, so only part of it is sorted or filtered.
- Totals that disappear. A total row inside the filtered range can be hidden by the filter. Keep totals outside.
- Forgetting a filter is on. Hidden rows still exist, and copied formulas or
SUMinclude them. Check for blue row numbers. - Sorting a list with merged cells. Excel refuses. Unmerge first.
Key points
- Click one cell in the list and use Data > Sort so Excel moves whole rows together.
- Add levels in the Sort dialog to sort by several columns; use Custom List for orders like months or priorities.
- Ctrl+Shift+L turns filters on and off; filters on different columns combine with AND.
SUBTOTAL(109, range)totals only visible rows; plainSUMtotals everything.- The
SORTandFILTERfunctions give live, separate results; the menu commands change the view of the data itself.
Exercise
Using the staff list: (1) sort it by City A to Z and then by Joined date, newest first; (2) filter to show only staff who joined in 2022 or later; (3) write a formula above the list that shows the average salary of the visible rows only, and say what it shows while your filter is on.
Show solution
- Data > Sort: sort by City, A to Z; Add Level, then by Joined, Newest to Oldest.
- Filter the Joined column with Date Filters > After or Equal To and enter 01-Jan-2022.
- In a cell outside the list that the filter cannot hide, such as
H1:
=SUBTOTAL(101, F2:F8)33875
Four people joined in 2022 or later: Isha (32000), Priya (31000), Ananya (38000) and Kabir (34500). Their average is 135500 ÷ 4 = 33875. AVERAGE on the same range would ignore the filter and return 37500.
Quick Quiz
1. You select only the Salary column and choose "Continue with the current selection". What happens?
Only that column moves, so every other column keeps its old order and the rows no longer match.
2. Which formula totals only the rows that a filter leaves visible?
SUBTOTAL ignores rows hidden by a filter; SUM adds every row.
3. Which shortcut turns filter arrows on or off?
Ctrl+Shift+L toggles the filter drop-downs on the heading row.
4. How do you sort months in calendar order instead of alphabetical order?
Custom lists such as January to December define an order that is not alphabetical.
Learn Excel with a mentor
Free lessons get you started. Our Excel & Office Skills training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
