Skip to content

SORT

Returns a sorted copy of a range or array. The original data stays where it is, and the result spills.

Syntax

=SORT(, , , )

Dynamic arrays

Excel for Microsoft 365, Excel 2021 and later, and Excel for the web. Not available in Excel 2019 or earlier.

Try it

Edit the formula in E2 or change the inputs. The sheet recalculates as you type.

E2
A
B
C
D
E
F
G
1
Product
Category
Units sold
2
Desk lamp
Lighting
340
Cable tidy
Cables
520
3
Monitor arm
Desks
215
Desk lamp
Lighting
340
4
Laptop stand
Desks
410
Laptop stand
Desks
410
5
USB-C hub
Cables
275
Monitor arm
Desks
215
6
Webcam
Video
180
USB-C hub
Cables
275
7
Cable tidy
Cables
520
Webcam
Video
180
8

E2 =SORT(A2:C7) returns Cable tidy, Cables, 520; Desk lamp, Lighting, 340; Laptop stand, Desks, 410; Monitor arm, Desks, 215; USB-C hub, Cables, 275; Webcam, Video, 180

How SORT works

SORT returns your data in a new order without touching the original. Because the result is a formula, it re-sorts itself whenever the source changes, which the Sort button on the Data tab never does.

The arguments

  • array is the block to sort. Every column in it comes back, in the same row order as the sort.
  • sort_index says which column of that block to sort by. It counts inside the block: for A2:C7, 3 means the third column, C. If you leave it out, SORT uses the first column.
  • sort_order is 1 for A to Z and smallest first, or -1 for the reverse.
  • by_col is rarely needed. Set it to TRUE when your data runs across a row, such as months in B1:G1, and you want to reorder the columns.

Sorting by several columns

Give sort_index and sort_order as array constants. =SORT(A2:C7,{2,3},{1,-1}) sorts by the second column A to Z and, inside each group, by the third column largest first. The two arrays must be the same length.

Sorting a calculated result

SORT accepts any array, so you can sort the output of another dynamic array function. =SORT(FILTER(A2:C7,C2:C7>200),3,-1) keeps the rows over 200 and then ranks them. =SORT(UNIQUE(B2:B7)) gives an alphabetical list of the categories.

SORT or SORTBY

SORT picks a column inside the array you are returning. If you want to sort by something that isn't part of the result, such as a column you don't want to show, or a calculated value like text length, use SORTBY instead.

What SORT does and doesn't do

Numbers sort before text, and text sorts alphabetically without regard to case. SORT moves whole rows together, so a name never loses its figure. It returns values, so number formats and fills from the source don't follow the result: format the spill range yourself.

The result is a spill range. If the cells it needs aren't empty you get #SPILL!. See FILTER for how spills work and how to read them with the # operator, as in =SUM(E2#).

If you only want to change the source order once, use the Sort button instead. SORT is for reports and summaries that must stay in order as the data changes.

Arguments

array
The range or array to sort, such as A2:C7. Leave out the header row, or it gets sorted along with the data.
sort_indexoptional
Which column of array to sort by, counted from 1 within the array, not by sheet column letter. The default is 1. Give several at once with an array constant such as {2,3} to sort by more than one column.
sort_orderoptional
1 for ascending, the default, or -1 for descending. With several sort columns, give one order each, such as {1,-1}.
by_coloptional
FALSE, the default, sorts the rows. TRUE sorts the columns, using a row of the array as the key.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

Alphabetical order by the first column

E2 =SORT(A2:C7)

Result: Cable tidy, Cables, 520; Desk lamp, Lighting, 340; Laptop stand, Desks, 410; Monitor arm, Desks, 215; USB-C hub, Cables, 275; Webcam, Video, 180

With only the array given, SORT orders by the first column, A to Z. Each row moves as a unit.

E2
A
B
C
D
E
F
G
1
Product
Category
Units sold
2
Desk lamp
Lighting
340
Cable tidy
Cables
520
3
Monitor arm
Desks
215
Desk lamp
Lighting
340
4
Laptop stand
Desks
410
Laptop stand
Desks
410
5
USB-C hub
Cables
275
Monitor arm
Desks
215
6
Webcam
Video
180
USB-C hub
Cables
275
7
Cable tidy
Cables
520
Webcam
Video
180
8

E2 =SORT(A2:C7) returns Cable tidy, Cables, 520; Desk lamp, Lighting, 340; Laptop stand, Desks, 410; Monitor arm, Desks, 215; USB-C hub, Cables, 275; Webcam, Video, 180

Example 2

Largest first: sort by the third column, descending

E2 =SORT(A2:C7,3,-1)

Result: Cable tidy, Cables, 520; Laptop stand, Desks, 410; Desk lamp, Lighting, 340; USB-C hub, Cables, 275; Monitor arm, Desks, 215; Webcam, Video, 180

sort_index 3 is the Units sold column of A2:C7, and -1 puts the biggest number on top.

E2
A
B
C
D
E
F
G
1
Product
Category
Units sold
2
Desk lamp
Lighting
340
Cable tidy
Cables
520
3
Monitor arm
Desks
215
Laptop stand
Desks
410
4
Laptop stand
Desks
410
Desk lamp
Lighting
340
5
USB-C hub
Cables
275
USB-C hub
Cables
275
6
Webcam
Video
180
Monitor arm
Desks
215
7
Cable tidy
Cables
520
Webcam
Video
180
8

E2 =SORT(A2:C7,3,-1) returns Cable tidy, Cables, 520; Laptop stand, Desks, 410; Desk lamp, Lighting, 340; USB-C hub, Cables, 275; Monitor arm, Desks, 215; Webcam, Video, 180

Example 3

Two sort keys: category A to Z, then units largest first

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

Result: Cable tidy, Cables, 520; USB-C hub, Cables, 275; Laptop stand, Desks, 410; Monitor arm, Desks, 215; Desk lamp, Lighting, 340; Webcam, Video, 180

The first key groups the categories. Inside Cables and Desks, the second key puts the larger sales first.

E2
A
B
C
D
E
F
G
1
Product
Category
Units sold
2
Desk lamp
Lighting
340
Cable tidy
Cables
520
3
Monitor arm
Desks
215
USB-C hub
Cables
275
4
Laptop stand
Desks
410
Laptop stand
Desks
410
5
USB-C hub
Cables
275
Monitor arm
Desks
215
6
Webcam
Video
180
Desk lamp
Lighting
340
7
Cable tidy
Cables
520
Webcam
Video
180
8

E2 =SORT(A2:C7,{2,3},{1,-1}) returns Cable tidy, Cables, 520; USB-C hub, Cables, 275; Laptop stand, Desks, 410; Monitor arm, Desks, 215; Desk lamp, Lighting, 340; Webcam, Video, 180

Example 4

Rank only the rows that pass a test

E2 =SORT(FILTER(A2:C7,C2:C7>200),3,-1)

Result: Cable tidy, Cables, 520; Laptop stand, Desks, 410; Desk lamp, Lighting, 340; USB-C hub, Cables, 275; Monitor arm, Desks, 215

FILTER drops the Webcam row (180 units). SORT then ranks the five that remain.

E2
A
B
C
D
E
F
G
1
Product
Category
Units sold
2
Desk lamp
Lighting
340
Cable tidy
Cables
520
3
Monitor arm
Desks
215
Laptop stand
Desks
410
4
Laptop stand
Desks
410
Desk lamp
Lighting
340
5
USB-C hub
Cables
275
USB-C hub
Cables
275
6
Webcam
Video
180
Monitor arm
Desks
215
7
Cable tidy
Cables
520
8

E2 =SORT(FILTER(A2:C7,C2:C7>200),3,-1) returns Cable tidy, Cables, 520; Laptop stand, Desks, 410; Desk lamp, Lighting, 340; USB-C hub, Cables, 275; Monitor arm, Desks, 215

Example 5

A sorted list of distinct values

E2 =SORT(UNIQUE(B2:B7))

Result: Cables; Desks; Lighting; Video

UNIQUE removes the repeats from the Category column, and SORT puts what is left in alphabetical order.

E2
A
B
C
D
E
F
G
1
Product
Category
Units sold
2
Desk lamp
Lighting
340
Cables
3
Monitor arm
Desks
215
Desks
4
Laptop stand
Desks
410
Lighting
5
USB-C hub
Cables
275
Video
6
Webcam
Video
180
7
Cable tidy
Cables
520
8

E2 =SORT(UNIQUE(B2:B7)) returns Cables; Desks; Lighting; Video

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In E2, list all invoices (three columns) with the most overdue first.

Answer in E2.

E2
A
B
C
D
E
F
G
1
Invoice
Customer
Days overdue
2
INV-301
Harbour Cafe
12
3
INV-302
Pine Dental
45
4
INV-303
Harbour Cafe
3
5
INV-304
Bright Print
28
6
INV-305
Pine Dental
9
7
INV-306
Bright Print
61
8
INV-307
Harbour Cafe
20
9
10
11
12
13
14
15
16
17

Common mistakes

  • sort_index counts columns inside array, not sheet columns. In =SORT(C2:E9,2) the number 2 means column D. Count from the first column of what you pass in.
  • Include the header row in array and it is sorted like data, ending up in the middle of the list. Start the range on the first data row, and put the headers in a separate row above the spill.
  • #VALUE! means sort_index is larger than the number of columns, or sort_order is something other than 1 or -1. Check both numbers.
  • The sorted copy has no formats. A date column shows as serial numbers until you format the spill range as dates.
  • Blank cells in the sort column are returned as 0 and can sort to an unexpected place. Fill them or filter them out first.

Questions people ask

How do I sort by more than one column with SORT?

Use array constants for the index and the order: `=SORT(A2:C7,{2,3},{1,-1})` sorts by column 2 ascending, then column 3 descending.

What is the difference between SORT and SORTBY?

SORT sorts by a column inside the array it returns. SORTBY sorts by any range of the same height, including one that isn't in the result or that you calculate on the fly.

Does SORT change my original data?

No. It returns a sorted copy and leaves the source as it was. If the source changes, the sorted copy updates.

Why does SORT show #SPILL!?

The cells below or beside the formula aren't empty, so there is no room for the result. Clear them or move the formula to a clear area.

How do I sort a row of data left to right?

Set the fourth argument to TRUE: `=SORT(B1:G1,1,-1,TRUE)` sorts the values in B1:G1 largest to smallest across the row.

Does SORT work in older versions of Excel?

No. It needs Microsoft 365, Excel 2021 or later, or Excel for the web. In Excel 2019 and earlier, use the Sort button on the Data tab.

Learn SORT in a real sheet.

SORT is taught in Data analysis with formulas, lesson 11: SORT and SORTBY. Each lesson is a short, graded exercise like the one above.