Cellwise Start free

Excel guide · 6 min read

How to sort by multiple columns in Excel

Quick answerClick any cell in the data, go to Data > Sort, choose the first column under Sort by, then click Add Level and choose the second column under Then by. Excel sorts by the first level and uses the next level only to order rows that tie. In Excel 365 and 2021 you can do the same with a formula: =SORTBY(A2:E10,B2:B10,1,D2:D10,1).

Sample data

Every result on this page was calculated from this sheet by the same spreadsheet engine the Cellwise app uses.

ABCDE
1TaskTeamPriorityDueHours
2Update price listSalesHighNov 12, 20263
3Renew SSL certificateITHighNov 9, 20261
4Q4 forecastFinanceMediumNov 20, 20266
5Laptop refreshITLowDec 4, 20268
6Client QBR deckSalesMediumNov 9, 20264
7Close October booksFinanceHighNov 6, 202610
8Backup testITMediumNov 9, 20262
9Lead list cleanupSalesLowNov 27, 20265
10Expense auditFinanceLowNov 20, 20263

Download this sheet as an Excel practice file (.xlsx)

The Sort dialog: one level per column

The task list should read team by team, with each team's tasks in due-date order. That is two sort keys: Team first, then Due. The A to Z and Z to A buttons only sort by one column, so you need the full Sort dialog, which lets you stack up to 64 levels.

Order matters. The top level is the main grouping; each level below only breaks ties left by the levels above. After this sort Finance comes first, starting with Close October books (6 Nov), then IT, then Sales. Swap the two levels and you get a list in date order where the teams are mixed together, which answers a different question.

  1. Click any single cell inside the data. Do not select one column, or Excel will ask whether to expand the selection.
  2. Go to Data > Sort.
  3. Check that My data has headers is ticked, so row 1 stays on top.
  4. Under Sort by choose Team, Sort On Cell Values, Order A to Z.
  5. Click Add Level. Under Then by choose Due, Order Oldest to Newest.
  6. Click OK.

Why sorting one column is not enough

Sorting by Team alone groups the rows but says nothing about the order inside each group. The formula below is the formula version of clicking Sort A to Z on the Team column. Each team is together, but here within Finance the Q4 forecast (due 20 Nov) sits above Close October books (due 6 Nov).

People often try to fix this by sorting Team, then sorting Due. The second sort reorders the whole list by date and breaks up the teams again. Use levels in one sort instead.

=SORT(A2:E10,2,1)

Result: spills 9 rows × 5 columns

Q4 forecastFinanceMedium463466
Close October booksFinanceHigh4633210
Expense auditFinanceLow463463
Renew SSL certificateITHigh463351
Laptop refreshITLow463608
Backup testITMedium463352
Update price listSalesHigh463383
Client QBR deckSalesMedium463354
Lead list cleanupSalesLow463535

The teams are grouped (Finance, IT, Sales), but the due dates inside each team are not in order: Q4 forecast on 20 Nov is listed before Close October books on 6 Nov.

Add a third level to break ties

Two levels are not always enough. IT has two tasks due on 9 Nov, and Finance has two due on 20 Nov. With only Team and Due as keys, Excel has no rule for which of each pair comes first, so the order of those rows is not something you chose.

Count the ties before you decide how many levels you need. This COUNTIFS checks how many rows share row 3's team and due date. Fill it down: any result above 1 is a tie. Across the whole list, =SUMPRODUCT(--(COUNTIFS(B2:B10,B2:B10,D2:D10,D2:D10)>1)) returns 4, meaning four rows are in a tie. Add a third level, such as Priority or Hours, to settle them.

=COUNTIFS(B2:B10,B3,D2:D10,D3)

Result: 2

2: Renew SSL certificate shares IT and 9 Nov with Backup test, so a third sort level is needed to fix their order.

Sort High, Medium, Low in that order

Sorting Priority A to Z gives High, Low, Medium, because it is alphabetical. For an order that is not alphabetical, use a custom list in the Order box. Excel remembers the list, so you only create it once per computer.

If you would rather not create a custom list, add a helper column with a rank number and sort by that column instead. MATCH finds each priority's position in a fixed list, so High is 1, Medium 2 and Low 3. Fill it down, sort by it smallest to largest, and the helper works in every Excel version and survives being emailed to someone else.

  1. In the Sort dialog, add a level for Priority.
  2. In its Order box choose Custom List.
  3. Under List entries type High, Medium, Low, each on its own line, then click Add and OK.
  4. Move the Priority level to where it belongs with the up and down arrows, then click OK.
=MATCH(C2,{"High","Medium","Low"},0)

Result: 1

1, because Update price list is High, the first item in the list. Medium rows get 2 and Low rows get 3.

A formula that stays sorted: SORTBY and SORT (Excel 365 / 2021)

The Sort dialog rearranges the data once. If new tasks keep arriving and you want a view that re-sorts itself, use a formula on another sheet or to the right of the data. =SORTBY(A2:E10,B2:B10,1,D2:D10,1) sorts the rows by Team A to Z, then by Due oldest first. Each extra key is another pair of range and order, where 1 is ascending and -1 descending.

SORT does the same with arrays: =SORT(A2:E10,{2,4},{1,1}) sorts by the 2nd column, then the 4th. SORTBY is usually easier to read and can sort by a column you do not return, and it handles a custom order too: =SORTBY(A2:E10,B2:B10,1,MATCH(C2:C10,{"High","Medium","Low"},0),1,D2:D10,1) sorts by team, then priority, then due date. These array forms are valid in Excel 365 and 2021, but the Cellwise practice engine cannot run them, so try them in Excel. In Excel 2019 and earlier, neither function exists; use the Sort dialog.

Keep the header row out of the sort

If you include row 1 in a SORT formula, or untick My data has headers in the dialog, the header is sorted like any other row. Here "Team" sorts after "Sales", so the header row lands at the bottom of the result.

In a formula, start the range at row 2 and type the headers above it. In the dialog, keep My data has headers ticked; Excel guesses this setting from your data, so check it every time, especially when the header and the data are both text.

=SORT(A1:E10,2,1)

Result: spills 10 rows × 5 columns

Q4 forecastFinanceMedium463466
Close October booksFinanceHigh4633210
Expense auditFinanceLow463463
Renew SSL certificateITHigh463351
Laptop refreshITLow463608
Backup testITMedium463352
Update price listSalesHigh463383
Client QBR deckSalesMedium463354
Lead list cleanupSalesLow463535
TaskTeamPriorityDueHours

The header row Task, Team, Priority, Due, Hours ends up as the last row of the result, because "Team" sorts after "Sales".

Common mistakes

Selecting one column and choosing Continue with the current selection

Only that column moves, so every task now sits next to the wrong team and date. Press Ctrl+Z straight away. Next time click a single cell, or choose Expand the selection.

A blank row or column in the middle of the data

Excel treats the blank as the edge of the data and only sorts part of it. Delete the blank row or column, or select the whole range before Data > Sort.

Dates or numbers stored as text

Text sorts separately from real dates and numbers, so the order looks random. Excel may show a Sort Warning about numbers stored as text. Convert the column to real values first.

Merged cells in the range

Excel refuses with a message that merged cells need to be the same size. Unmerge them (Home > Merge & Center) and use Center Across Selection for the look instead.

FAQ

How do I sort by two columns in Excel?

Go to Data > Sort, set the first column under Sort by, click Add Level, and set the second column under Then by. Excel sorts by the first column and uses the second only where the first one ties.

How many columns can I sort by in Excel?

The Sort dialog accepts up to 64 levels. In practice two or three are enough; add another only when rows still tie on the levels you have.

How do I sort by date and then by name?

Use Data > Sort with Due (Oldest to Newest) as the first level and the name column (A to Z) as the second. If the dates sort in a strange order, they are probably text, not real dates.

Why did sorting mix up my data?

Usually only one column was selected and Excel sorted just that column, or a blank row stopped the sort partway. Undo with Ctrl+Z, click a single cell inside the data and sort again from Data > Sort.

Can I sort columns left to right instead of rows?

Yes. In the Sort dialog click Options and choose Sort left to right, then pick the row to sort by. In a formula, SORT has a by_col argument for the same job.

Functions used

Related guides

Updated