Confident CellsExcel practice Start free

SORT and SORTBY: sorted lists that keep up

Updated 26 November 2026 · 4 min read

=SORT(A2:B9,2,-1) gives a copy of the table sorted by its 2nd column, biggest first, and the copy sorts itself again whenever the numbers change. =SORTBY(A2:A9,B2:B9,-1) sorts one column by another. Both are in Excel for Microsoft 365 (Windows and Mac), Excel 2021 and 2024, and Excel for the web. Excel 2019 and older show #NAME?, and Google Sheets has SORT but not SORTBY.

The formulas at a glance

You wantFormulaWhat you get
Biggest first=SORT(A2:B6,2,-1)The table sorted by its 2nd column, largest at the top
A to Z=SORT(A2:B6)The table sorted by its 1st column
Only the names, best first=SORTBY(A2:A6,B2:B6,-1)The names in order of sales, without the sales
By two columns=SORT(A2:C7,{1,3},{1,-1})Team A to Z, then biggest sales first in each team
Some rows, sorted=SORT(FILTER(B2:C7,A2:A7="North"),2,-1)Only the North team, biggest first
A top 3=TAKE(SORT(A2:B6,2,-1),3)The first 3 rows of the sorted table

Each one is worked through below. All of them spill: you type the formula in one cell and the answer fills the cells below.

SORT: three parts

  1. What to sort: the table, without its headings.
  2. Which column to sort by, counting from the left of the table: 2 is the second column.
  3. The order: 1 for smallest first (A to Z), -1 for biggest first (Z to A).

Five reps have sales of £4,200, £3,100, £5,600, £2,800 and £4,900. Biggest first, that's Cara (£5,600), Ewa (£4,900), Amira (£4,200), Ben (£3,100) and Dev (£2,800).

=SORT(A2:B6,2,-1)
ABCDE
1RepSalesSorted
2Amira£4,200.00Cara£5,600.00
3Ben£3,100.00Ewa£4,900.00
4Cara£5,600.00Amira£4,200.00
5Dev£2,800.00Ben£3,100.00
6Ewa£4,900.00Dev£2,800.00
D2 holds =SORT(A2:B6,2,-1): Cara, Ewa, Amira, Ben, Dev.

Leave out the last two parts and SORT uses the first column, A to Z: =SORT(A2:B6) puts the names in alphabetical order. Dates are numbers to Excel, so 1 puts the oldest date first and -1 the newest.

The original list doesn't move. Change Ben's sales to £6,000 and he jumps to the top of the sorted copy straight away, with no Sort button to press.

SORTBY: sort by a column you don't show

SORTBY takes what to show, then what to sort it by. To list just the names, best seller first:

=SORTBY(A2:A6,B2:B6,-1)
ABCD
1RepSalesBest first
2Amira£4,200.00Cara
3Ben£3,100.00Ewa
4Cara£5,600.00Amira
5Dev£2,800.00Ben
6Ewa£4,900.00Dev
D2 holds =SORTBY(A2:A6,B2:B6,-1). The names come out in order of sales, and the sales stay out of the answer.

SORT can only sort by a column that's in the answer. SORTBY can sort by any column of the same height, even one on another sheet.

Sorting by two columns

Six reps work in two teams. You want the teams A to Z, and within each team the biggest sales first. North's reps sold £4,200, £5,600 and £4,900, so North comes out as Cara, Ewa, Amira. South's sold £3,100, £2,800 and £3,900, so South comes out as Finn, Ben, Dev.

Give SORT the columns in curly brackets, and an order for each: column 1 (team) A to Z, then column 3 (sales) biggest first.

=SORT(A2:C7,{1,3},{1,-1})
ABCDEFG
1TeamRepSalesTeamRepSales
2NorthAmira£4,200.00NorthCara£5,600.00
3SouthBen£3,100.00NorthEwa£4,900.00
4NorthCara£5,600.00NorthAmira£4,200.00
5SouthDev£2,800.00SouthFinn£3,900.00
6NorthEwa£4,900.00SouthBen£3,100.00
7SouthFinn£3,900.00SouthDev£2,800.00
E2 holds =SORT(A2:C7,{1,3},{1,-1}). North comes first, A to Z, and each team is sorted biggest first.

SORTBY does the same with pairs, which some people find easier to read: =SORTBY(A2:C7,A2:A7,1,C2:C7,-1). Each pair is a column and its order.

Sorting some of the rows

To sort only the rows that match, put FILTER inside SORT. FILTER keeps the North rows, and SORT puts them in order:

=SORT(FILTER(B2:C7,A2:A7="North"),2,-1)
ABCDEF
1TeamRepSalesNorth, best first
2NorthAmira£4,200.00Cara£5,600.00
3SouthBen£3,100.00Ewa£4,900.00
4NorthCara£5,600.00Amira£4,200.00
5SouthDev£2,800.00
6NorthEwa£4,900.00
7SouthFinn£3,900.00
E2 holds =SORT(FILTER(B2:C7,A2:A7="North"),2,-1): Cara, Ewa, Amira. The 2 is the second column of what FILTER gives back, the sales.

The column number counts across what FILTER hands over (Rep and Sales), so sales is column 2 here, even though it's column C on the sheet.

A top 3

Put SORT inside TAKE to keep the first 3 rows (see DROP, TAKE and CHOOSEROWS):

=TAKE(SORT(A2:B6,2,-1),3)
ABCDE
1RepSalesTop 3
2Amira£4,200.00Cara£5,600.00
3Ben£3,100.00Ewa£4,900.00
4Cara£5,600.00Amira£4,200.00
5Dev£2,800.00
6Ewa£4,900.00
D2 holds =TAKE(SORT(A2:B6,2,-1),3): Cara (£5,600), Ewa (£4,900) and Amira (£4,200).

For one person's position instead, RANK is simpler: =RANK(B2,$B$2:$B$6) gives Amira 3. There's more in RANK and a top 5.

To add up a group before sorting, such as sales for each team biggest first, GROUPBY totals and sorts in one formula. For a sorted list with no repeats, use =SORT(UNIQUE(A2:A20)). To sort two lists as one, stack them first with VSTACK: =SORT(VSTACK(A2:B6,D2:E6),2,-1).

SORT or the Sort button?

The Sort button on the Data tab (Data › Sort, in the same place in Excel for Mac) sorts the data where it is, once. Use it to tidy a list for good. Use SORT when you want the original left as it is, or a sorted copy that keeps up when people add or change rows.

To turn a SORT answer into plain values you can edit, copy it with Ctrl+C (Cmd+C on a Mac), then paste it back with Paste Special and pick Values. Paste Special is Ctrl+Alt+V on Windows and Cmd+Control+V in Excel for Mac.

Common mistakes

Headings sorted into the list

The range started at row 1, so the heading was sorted with the names. Start at row 2, and type the headings above the formula yourself.

Numbers in a strange order

If 100 comes before 25, the numbers are stored as text, which sorts like words. Fix them first: see numbers stored as text.

Sorting by the wrong column

The column number counts from the left of the range. In SORT(C2:E9,2), column 2 is D. A number bigger than the table, such as 4 for a table 3 columns wide, gives #VALUE!.

#SPILL!

Something is in the cells the sorted copy needs. Clear the space below and to the right (see the #SPILL! error).

Try it now

Your turn

In D2, use SORT to list the products and units sold with the best seller first.

ABCDE
1ProductUnits soldBest sellers first
2Pens140
3Folders85
4Labels40
5Staples95
6Paper210
7Envelopes60

Open this in Try a formula: the same sheet with the answer filled in, to change or share by link.

Part of Excel data analysis practice: every lesson on the topic, with a project in Excel.

Where it works

SORT and SORTBY are in Excel for Microsoft 365 on Windows and Mac, Excel 2021 and 2024, and Excel for the web. Excel 2019 and older show #NAME?: use the Sort button there. Google Sheets has its own SORT, which takes TRUE or FALSE for the order.

Questions

How do I sort by two columns with the SORT function?

Put the column numbers in curly brackets, with an order for each: =SORT(A2:C7,{1,3},{1,-1}) sorts by column 1 A to Z, then by column 3 biggest first within each group. SORTBY does the same with pairs of a column and its order: =SORTBY(A2:C7,A2:A7,1,C2:C7,-1). Both give the same answer, so use whichever you find easier to read.

What is the difference between SORT and SORTBY?

SORT sorts by one of the columns you're showing, given as a number counted from the left of the range. SORTBY sorts by a range you give it, which doesn't have to be shown. So =SORTBY(A2:A6,B2:B6,-1) lists only the names, in order of sales. SORTBY is also safer if columns get added later, because the range moves with them.

Why does SORT show #NAME? in my Excel?

Your version doesn't have SORT. It's in Excel for Microsoft 365, Excel 2021 and 2024, and Excel for the web, on Windows and Mac. Excel 2019 and older show #NAME?. Use the Sort button on the Data tab there instead, which sorts the list once and doesn't update. A spelling slip such as SROT gives #NAME? too.

How do I sort dates with the SORT function?

Dates are stored as numbers, so they sort like numbers: =SORT(A2:C20,1) puts the oldest date in column 1 first, and =SORT(A2:C20,1,-1) puts the newest first. If the dates come out in a muddle, some are probably text, such as dates pasted from another system. Those sit on the left of their cells. Turn them into real dates first.