Excel functions · Dynamic arrays

SORT function in Excel

SORT returns a sorted copy of a range, by one or more of its own columns, without changing the original data. =SORT(A2:E9, 5, -1) lists the orders from largest to smallest amount, and the list re-sorts itself when the data changes.

Last reviewed

Syntax

=SORT(array, [sort_index], [sort_order], [by_col])
arrayrequired
The range to sort.
sort_indexoptional
Which column of array to sort by (1 is the first). Use an array such as {3,5} for several columns.
sort_orderoptional
1 ascending (the default) or -1 descending. Use an array such as {1,-1} to match several columns.
by_coloptional
FALSE sorts rows (the default); TRUE sorts columns left to right.
  • The result spills: type the formula in one cell and it fills as many cells as it needs, and it updates when the data changes.
  • To sort by a column you don't want to return, use SORTBY.

Which versions have SORT

Microsoft 365, Excel for the web, and Excel 2021 or later (Windows and Mac). Not in Excel 2019 or 2016.

SORT availability by Excel version
VersionHas it
Microsoft 365 (Windows and Mac)Yes
Excel for the webYes
Excel 2024Yes
Excel 2021Yes
Excel 2019No
Excel 2016No

Google Sheets

Google Sheets has SORT with different arguments: SORT(range, sort_column, is_ascending, [sort_column2, is_ascending2, ...]). Each sort column needs its own TRUE/FALSE, and the column can also be a separate range.

Examples

The examples use this sample sheet.

Orders (A1:E9)
RowABCDE
1Order IDRepRegionProductAmount
21001AnaNorthLaptop1200
31002BenSouthMonitor300
41003AnaNorthMonitor350
51004CaraEastLaptop1150
61005BenSouthLaptop1250
71006DevWestKeyboard80
81007CaraEastMonitor320
91008AnaSouthKeyboard90
  1. 1.Largest orders first

    =SORT(A2:E9, 5, -1)

    Result: Spills 8 rows, from 1005 (1,250) down to 1006 (80)

    1005BenSouthLaptop1250
    1001AnaNorthLaptop1200
    1004CaraEastLaptop1150
    1003AnaNorthMonitor350
    1007CaraEastMonitor320
    1002BenSouthMonitor300
    1008AnaSouthKeyboard90
    1006DevWestKeyboard80

    Column 5 is Amount; -1 sorts it from largest to smallest.

  2. 2.A sorted list of unique names

    =SORT(UNIQUE(B2:B9))

    Result: Spills Ana, Ben, Cara, Dev

    Ana
    Ben
    Cara
    Dev

    UNIQUE removes the repeats and SORT puts them in A–Z order.

  3. 3.Two sort levels

    =SORT(A2:E9, {3,5}, {1,-1})

    Result: Spills 8 rows: by region A–Z, then by amount, largest first

    1004CaraEastLaptop1150
    1007CaraEastMonitor320
    1001AnaNorthLaptop1200
    1003AnaNorthMonitor350
    1005BenSouthLaptop1250
    1002BenSouthMonitor300
    1008AnaSouthKeyboard90
    1006DevWestKeyboard80

    Region (column 3) ascending first, then Amount (column 5) descending within each region.

Common errors and fixes

SORT errors, causes and fixes
ErrorWhy it happensFix
#VALUE!sort_index is larger than the number of columns in array, or less than 1.Count the columns of array, not of the sheet.
#SPILL!The result needs more cells than are free: something is in the way, or the formula is inside an Excel table.Clear the cells below and to the right, or move the formula outside the table.
#NAME?Your Excel doesn't have dynamic array functions (Excel 2019 or older).Open the file in Microsoft 365, Excel for the web, or Excel 2021 or later.

Do it with GPTExcel

Skip writing SORT by hand

Upload your Excel or CSV file to Chat and ask in plain English. GPTExcel writes the formula for your file and your version of Excel or Google Sheets.

your-file.xlsx

Make a live leaderboard of reps sorted by total sales.

Done. I used SORT on your sheet and checked the results against your data.

your-file-updated.xlsxYour original stays unchanged
  • Written for your platform and version
  • Checked against your actual data
  • Newest functions looked up in official docs
Try it free Formula Generator

Free plan, no credit card.

SORT questions

Does SORT change my original data?
No. SORT returns a sorted copy somewhere else; the source range stays as it is.
What's the difference between SORT and SORTBY?
SORT sorts by a column number inside the range you return. SORTBY sorts by any range of the same height, even one you don't return, and lists each sort level as a range and an order.

Skip the formula hunt

Upload a spreadsheet and ask in plain English. Free plan, no credit card.

Start free