Sort a list with SORT and SORTBY

Lists that spill · About 5 minutes

Sort a list with SORT and SORTBY

SORT gives back a list in order. =SORT(A2:A8) sorts A to Z. To sort a table by one of its columns, say which column and which way: =SORT(A2:B8,2,-1) sorts by the 2nd column, highest first. 1 means lowest first. In Google Sheets, SORT takes TRUE or FALSE instead: =SORT(A2:B8,2,FALSE) sorts highest first.

SORTBY sorts one list by the values in another: =SORTBY(A2:A8,B2:B8,-1) lists only the names, in order of their marks, highest first. Google Sheets has no SORTBY; there, =SORT(A2:A8,B2:B8,FALSE) does the same.

These work in Excel 365 and Excel 2021 or later. Google Sheets has SORT but not SORTBY. Excel 2016 and 2019 don't have them.

Part of Plus

Free during our founding launch: sign in to unlock this lesson's exercises, the timed practice sets and the rest of Plus. The free lessons stay free, with no account needed.

What SORT and SORTBY does

SORT gives back a list or a table in order, A to Z or lowest to highest, or the other way round. SORTBY puts one list in the order of another, like names in order of their hours, without showing the hours.

Both spill a new, sorted copy and leave the original alone. When the data changes, the sorted copy changes with it, so a leaderboard or a rota stays in order by itself.

How to write SORT and SORTBY

=SORT(array, [sort_index], [sort_order], [by_col])

array
The list or table to sort.
sort_index
Which column of the table to sort by, counting from 1. Left out, it sorts by the first column.
sort_order
1 for lowest first or A to Z, -1 for highest first. Left out, it is 1.
by_col
Optional. TRUE sorts columns left to right instead of rows. Most lists leave it out.

=SORTBY(array, by_array1, [sort_order1], ...)

by_array1
The cells to sort by. They must cover the same rows as the array, but don't have to be part of it.
sort_order1
1 or -1, as in SORT. You can add more pairs to break ties.

SORT and SORTBY worked example

In C2, list the tasks with their hours, shortest first.

In C2, list the tasks with their hours, shortest first.
ABCD
1TaskHoursTaskHours
2Plan3Email1
3Survey8Plan3
4Email1Review5
5Design12Survey8
6Review5Design12
C2=SORT(A2:B6,2,1)Sort the table A2 to B6 by its 2nd column, the hours, lowest first: Email, Plan, Review, Survey and Design spill into C2 to D6 with their hours.

Common SORT and SORTBY mistakes

Highest first doesn't work in Google Sheets

Google Sheets reads the third part as TRUE or FALSE, not 1 or -1. Write FALSE there for highest first.

Names no longer match their numbers

Sorting one column on its own breaks the pairs. Sort the whole table, like A2:B6, or use SORTBY to sort the names by the numbers.

A #SPILL! error instead of a list

The sorted copy needs empty cells to land in. Clear the cells below and to the right of the formula.

SORT or SORTBY?

Use SORT when the column you sort by is part of what you want to show. Use SORTBY when you want only some columns back, sorted by one you don't show, or to sort by two columns at once.

SORT and SORTBY work in Microsoft 365 and Excel 2021 or later. Google Sheets has SORT, with TRUE or FALSE for the order, but not SORTBY: there, SORT accepts a separate range to sort by. The Sort button, by contrast, reorders the data itself, once.

Questions people ask

How do I sort with a formula in Excel?

=SORT(A2:A8) sorts A to Z. =SORT(A2:B8,2,-1) sorts the table by its 2nd column, highest first.

What is the difference between SORT and SORTBY?

SORT orders a range by one of its own columns. SORTBY orders one list by another range: =SORTBY(A2:A8,B2:B8,-1).

Does the sorted list update by itself?

Yes. Change a value in the source and the sorted list changes with it.

Theme