Skip to content

SORTBY

Sorts a range by the values in one or more other ranges, which do not have to be part of the result.

Syntax

=SORTBY(, , , [by_array2, sort_order2], …)

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
1
Task
Priority
Hours
2
Pay run
Medium
6
Audit pack
High
3
Audit pack
High
12
Update forecast
High
4
Stock count
Low
8
Stock count
Low
5
Renew licences
Medium
3
Pay run
Medium
6
Update forecast
High
10
Close ledger
High
7
Close ledger
High
5
Renew licences
Medium
8

E2 =SORTBY(A2:B7,C2:C7,-1) returns Audit pack, High; Update forecast, High; Stock count, Low; Pay run, Medium; Close ledger, High; Renew licences, Medium

How SORTBY works

SORTBY sorts one range by the values in another. That is the difference from SORT, which can only sort by a column inside the array it returns. With SORTBY, the sort key can be a column you don't want to show, a column in a different table, or a calculated array.

The arguments

  • array is what you want back.
  • by_array1 is what to sort by. It must be the same height as array, one column wide.
  • sort_order1 is 1 (ascending, the default) or -1 (descending).
  • After that, add more pairs: another key, then its order. Each extra key breaks ties in the ones before it.

=SORTBY(A2:B7,C2:C7,-1) returns columns A and B ordered by column C, largest first. Column C itself isn't in the result.

Sorting by a calculation

Because by_array can be any array, you can sort by something that isn't in the sheet. =SORTBY(A2:A7,LEN(A2:A7),-1) lists the tasks from longest name to shortest. =SORTBY(A2:A9,MONTH(B2:B9)) orders rows by calendar month, ignoring the year.

A custom order

Sorting alphabetically puts High before Low before Medium, which is rarely what you want. To sort in your own order, turn each item into its position in a list with MATCH:

=SORTBY(A2:B7,MATCH(B2:B7,{"High","Medium","Low"},0),1,A2:A7,1)

MATCH gives 1 for High, 2 for Medium and 3 for Low, SORTBY sorts by those numbers, and the second key puts the tasks in each priority in alphabetical order. Using more than one key also makes the result easy to predict when many rows share a value.

Shuffling

Sorting by random numbers shuffles a list: =SORTBY(A2:A7,RANDARRAY(ROWS(A2:A7))). The order changes every time the sheet recalculates. See RANDARRAY.

Rules to remember

Each by_array must be the same height as array. If it isn't, SORTBY returns #VALUE!. All the by_array arguments must also point the same way: either all columns or all rows. The result is a spill range, so the cells it needs must be empty or you get #SPILL!. Empty cells and header rows are treated like any other values, so leave the header out of both array and by_array.

Use SORT when the sort column is in the result, because it is shorter. Use SORTBY for everything else.

Arguments

array
The range or array to return, such as A2:A7.
by_array1
The values to sort by. It must be one column with the same number of rows as array (or one row with the same number of columns). It can be a range or a calculation such as LEN(A2:A7).
sort_order1optional
1 for ascending, the default, or -1 for descending.
by_array2, sort_order2, …optional
More sort keys, each followed by its own order. Excel applies them in turn, so the second key only breaks ties in the first.

Examples

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

Example 1

Sort by a column you do not return

E2 =SORTBY(A2:B7,C2:C7,-1)

Result: Audit pack, High; Update forecast, High; Stock count, Low; Pay run, Medium; Close ledger, High; Renew licences, Medium

Only the Task and Priority columns come back, but they are ordered by the Hours column, largest first.

E2
A
B
C
D
E
F
1
Task
Priority
Hours
2
Pay run
Medium
6
Audit pack
High
3
Audit pack
High
12
Update forecast
High
4
Stock count
Low
8
Stock count
Low
5
Renew licences
Medium
3
Pay run
Medium
6
Update forecast
High
10
Close ledger
High
7
Close ledger
High
5
Renew licences
Medium
8

E2 =SORTBY(A2:B7,C2:C7,-1) returns Audit pack, High; Update forecast, High; Stock count, Low; Pay run, Medium; Close ledger, High; Renew licences, Medium

Example 2

Two keys: priority A to Z, then hours largest first

E2 =SORTBY(A2:C7,B2:B7,1,C2:C7,-1)

Result: Audit pack, High, 12; Update forecast, High, 10; Close ledger, High, 5; Stock count, Low, 8; Pay run, Medium, 6; Renew licences, Medium, 3

High, Low and Medium are alphabetical. Within High, the second key puts 12 hours before 10 before 5.

E2
A
B
C
D
E
F
G
H
1
Task
Priority
Hours
2
Pay run
Medium
6
Audit pack
High
12
3
Audit pack
High
12
Update forecast
High
10
4
Stock count
Low
8
Close ledger
High
5
5
Renew licences
Medium
3
Stock count
Low
8
6
Update forecast
High
10
Pay run
Medium
6
7
Close ledger
High
5
Renew licences
Medium
3
8

E2 =SORTBY(A2:C7,B2:B7,1,C2:C7,-1) returns Audit pack, High, 12; Update forecast, High, 10; Close ledger, High, 5; Stock count, Low, 8; Pay run, Medium, 6; Renew licences, Medium, 3

Example 3

A custom order: High, then Medium, then Low

E2 =SORTBY(A2:B7,MATCH(B2:B7,{"High","Medium","Low"},0),1,A2:A7,1)

Result: Audit pack, High; Close ledger, High; Update forecast, High; Pay run, Medium; Renew licences, Medium; Stock count, Low

MATCH turns each priority into 1, 2 or 3, so SORTBY follows the order of the list. The second key sorts each priority group by task name.

E2
A
B
C
D
E
F
1
Task
Priority
Hours
2
Pay run
Medium
6
Audit pack
High
3
Audit pack
High
12
Close ledger
High
4
Stock count
Low
8
Update forecast
High
5
Renew licences
Medium
3
Pay run
Medium
6
Update forecast
High
10
Renew licences
Medium
7
Close ledger
High
5
Stock count
Low
8

E2 =SORTBY(A2:B7,MATCH(B2:B7,{"High","Medium","Low"},0),1,A2:A7,1) returns Audit pack, High; Close ledger, High; Update forecast, High; Pay run, Medium; Renew licences, Medium; Stock count, Low

Example 4

Sort by a calculated value

E2 =SORTBY(A2:A7,LEN(A2:A7),-1)

Result: Update forecast; Renew licences; Close ledger; Stock count; Audit pack; Pay run

LEN counts the characters in each task name, 15 for Update forecast down to 7 for Pay run. The lengths exist only inside the formula.

E2
A
B
C
D
E
F
1
Task
Priority
Hours
2
Pay run
Medium
6
Update forecast
3
Audit pack
High
12
Renew licences
4
Stock count
Low
8
Close ledger
5
Renew licences
Medium
3
Stock count
6
Update forecast
High
10
Audit pack
7
Close ledger
High
5
Pay run
8

E2 =SORTBY(A2:A7,LEN(A2:A7),-1) returns Update forecast; Renew licences; Close ledger; Stock count; Audit pack; Pay run

Your turn.

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

Task 1 of 2

In E2, list only the store names, with the highest-selling store first.

Answer in E2.

E2
A
B
C
D
E
F
G
1
Store
Region
Sales
2
Dock St
North
8,400
3
Mill Rd
South
6,100
4
High St
North
9,700
5
Park Ave
West
5,200
6
Quay Side
South
7,300
7
Elm Way
West
6,800
8
Union Sq
North
4,500
9
10
11
12
13
14
15
16
17

Common mistakes

  • The sort range and the data must be the same height. =SORTBY(A2:A9,B2:B8) returns #VALUE! because one range ends a row early. Check that both start and end on the same rows.
  • All the sort ranges must run the same way: all columns or all rows. Mixing a column key with a row key returns #VALUE!.
  • Alphabetical sorting is rarely the business order. Priorities, months and statuses need the MATCH-against-a-list technique to sort in the order people expect.
  • The header row must stay out of both array and every by_array. If one includes it and the other doesn't, the sizes disagree and you get #VALUE!.
  • Like every dynamic array result, the output spills. If the cells it needs are not empty, you get #SPILL!.

Questions people ask

When should I use SORTBY instead of SORT?

Use SORTBY when the column you sort by isn't part of what you return, when the key is a calculation, or when you need a custom order. Use SORT when the sort column is in the returned data.

Can SORTBY sort by more than one column?

Yes. Add more pairs of range and order: `=SORTBY(A2:C7,B2:B7,1,C2:C7,-1)` sorts by column B first, then column C largest first.

How do I sort in a custom order such as High, Medium, Low?

Sort by `MATCH(B2:B7,{"High","Medium","Low"},0)`. MATCH gives each row its position in your list, and SORTBY sorts by that position.

How do I shuffle a list in Excel?

Use `=SORTBY(A2:A7,RANDARRAY(ROWS(A2:A7)))`. It sorts the list by a column of random numbers, so the order changes at every recalculation.

Does SORTBY work in Excel 2019?

No. It needs Microsoft 365, Excel 2021 or later, or Excel for the web. In earlier versions the formula returns `#NAME?`.

Learn SORTBY in a real sheet.

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