Menu

Excel SORT and SORTBY Functions: Sort with a Formula

=SORT(A2:C7,3,-1) returns the table A2:C7 sorted by its third column, largest first, and keeps re-sorting as the data changes. SORTBY sorts by any range, including several columns and a custom order.

Every sheet on this page is live: change a number or a formula and it recalculates.

=SORT(A2:C7,3,-1) returns the table A2:C7 sorted by its third column, sales, from largest to smallest. The original table stays as it is; the sorted copy spills from the formula cell and re-sorts itself when a number changes. Change C7 to 300 and Finn moves to the top.

Sales from highest to lowest
E2
ABCDEFG
1NameRegionSalesNameRegionSales
2AnnNorth120CaraNorth200
3BenSouth80DanEast150
4CaraNorth200AnnNorth120
5DanEast150EveSouth95
6EveSouth95BenSouth80
7FinnNorth60FinnNorth60
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

SORT syntax

=SORT(array, [sort_index], [sort_order], [by_col])
  • array is the range to sort and return.
  • sort_index is the column number inside array to sort by. The default is 1.
  • sort_order is 1 for ascending (the default) or -1 for descending.
  • by_col is FALSE (the default) to sort rows, TRUE to sort columns left to right.

SORT and SORTBY need Excel 2021, Excel 2024 or Microsoft 365; older versions show #NAME?. Google Sheets has both, though its SORT takes a column number and TRUE or FALSE in pairs.

Ascending order puts numbers first, then text A to Z, then FALSE and TRUE. Text is compared without regard to case. Empty cells in array come back as 0 in the result.

How to alphabetize in Excel

To reorder a list in place, click any cell in it and choose Data > Sort A to Z (or Z to A). If the list has neighbouring columns, Excel sorts the whole table so each row stays together. Use Data > Sort for more than one column or a custom list.

To keep the original order and get an alphabetical copy that updates by itself, use SORT with no other arguments:

Names A to Z
C2
ABC
1NameA to Z
2MiaAnn
3BenBen
4ZoeCara
5AnnDan
6LeoLeo
7CaraMia
8DanZoe
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Type Abe in A3 and he appears first. =SORT(A2:A8,,-1) gives Z to A; the two commas skip sort_index. To alphabetize a list without repeats, sort the result of UNIQUE: =SORT(UNIQUE(A2:A8)) (see UNIQUE).

SORTBY: sort by a column you do not return

SORT can only sort by a column of the range it returns. SORTBY takes the sort key as a separate range, so you can return names alone, ordered by sales:

Names ordered by sales
E2
ABCDE
1NameRegionSalesBy sales
2AnnNorth120Cara
3BenSouth80Dan
4CaraNorth200Ann
5DanEast150Eve
6EveSouth95Ben
7FinnNorth60Finn
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

The result is Cara, Dan, Ann, Eve, Ben, Finn. The syntax is =SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...). Each by_array must have as many rows as array, otherwise SORTBY returns #VALUE!.

For a top 3, take the first rows of the result: =TAKE(SORTBY(A2:A7,C2:C7,-1),3) returns Cara, Dan and Ann. TAKE needs Microsoft 365 or Excel 2024; in Excel 2021 use =INDEX(SORTBY(A2:A7,C2:C7,-1),SEQUENCE(3)).

Sort by multiple columns

Give SORTBY one pair of key and order per level. This sorts by region A to Z, then within each region by sales from highest to lowest:

Region A to Z, then sales high to low
E2
ABCDEFG
1NameRegionSalesNameRegionSales
2AnnNorth120DanEast150
3BenSouth80CaraNorth200
4CaraNorth200AnnNorth120
5DanEast150FinnNorth60
6EveSouth95EveSouth95
7FinnNorth60BenSouth80
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

East comes first, then the three North rows as Cara (200), Ann (120) and Finn (60), then South. SORT can do the same with arrays of column numbers and orders, though SORTBY is easier to read:

=SORT(A2:C7,{2,3},{1,-1})

Sort in a custom order

Alphabetical is not always the order you want. To sort regions as North, East, South, turn each region into its position in that list with MATCH and sort by the positions:

North first, then East, then South
E2
ABCDEFG
1NameRegionSalesNameRegionSales
2AnnNorth120AnnNorth120
3BenSouth80CaraNorth200
4CaraNorth200FinnNorth60
5DanEast150DanEast150
6EveSouth95BenSouth80
7FinnNorth60EveSouth95
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

MATCH returns 1 for North, 2 for East and 3 for South, and SORTBY sorts by those numbers. Rows with the same key keep their original order, so the North rows stay Ann, Cara, Finn. The same trick sorts months or weekdays by name: list them in calendar order inside MATCH. A region missing from the list gets #N/A from MATCH; use IFERROR(MATCH(B2:B7,{"North","East","South"},0),99) as the key to put such rows last.

Practice: names ordered by sales

Your turn
E2
ABCDE
1NameRegionSalesBy sales
2AnnNorth120
3BenSouth80
4CaraNorth200
5DanEast150
6EveSouth95
7FinnNorth60
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: In E2, return the names only, ordered by sales from highest to lowest.

SORT function or the Sort button

Both sort, but they do different jobs:

Data > SortSORT and SORTBY
Changes the original tableYesNo, the result goes to new cells
Updates when data changesNo, sort againYes
Works in Excel 2019 and olderYesNo
Result can be edited cell by cellYesNo, it is one formula
Can be wrapped in another formulaNoYes, for example =TAKE(SORT(A2:C7,3,-1),3)

Use the button to tidy a table once. Use the function for a report or dashboard that has to stay sorted while the data changes. If the cells below or to the right of the formula are not empty, the function shows #SPILL! instead of the sorted list; clear them and it appears (see #SPILL! error).

Frequently Asked Questions

How do I sort data with a formula in Excel?

Use SORT: =SORT(A2:C7,3,-1) returns A2:C7 sorted by its third column, largest first. Leave out the last two arguments, =SORT(A2:A8), to sort by the first column from smallest to largest, or A to Z for text.

What is the difference between SORT and SORTBY?

SORT sorts by a column of the range it returns, picked by number. SORTBY sorts by any range you give it, even one you do not return: =SORTBY(A2:A7,C2:C7,-1) returns only names, ordered by sales. SORTBY also takes several sort keys directly.

How do I alphabetize a list in Excel?

Select a cell in the list and click Data > Sort A to Z to reorder it in place. To keep the original and get a sorted copy that updates, type =SORT(A2:A8) in an empty cell; use =SORT(A2:A8,,-1) for Z to A.

How do I sort by two columns with a formula?

Give SORTBY two pairs of key and order: =SORTBY(A2:C7,B2:B7,1,C2:C7,-1) sorts by region A to Z and, within each region, by sales from highest to lowest.

Which Excel versions have SORT and SORTBY?

Excel 2021, Excel 2024 and Microsoft 365. In Excel 2019 and older they show #NAME?, and the Sort button on the Data tab is the way to sort.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED