Dynamic Arrays

How to Sort Data With a Formula in Excel

Sort rows with SORT and SORTBY in Excel. The original data stays put, and the sorted list updates when the data changes. Covers sorting by several columns.

On this page
  1. The short answer
  2. The Sort button or a formula
  3. Sorting by a column you don’t show
  4. Sorting by more than one column
  5. Numbers stored as text
  6. Removing duplicates and sorting in one step
  7. Leave room for the result
  8. Choosing the range
  9. Google Sheets

The short answer

=SORT(array, [sort_index], [sort_order], [by_col]) returns the rows of a range in a new order, and the original rows stay where they are. For a simple sort, give the range and the number of the column to sort by:

=SORT(A2:C20, 3, -1)

This sorts rows A2:C20 by the third column, largest to smallest. The -1 means descending. The default, 1, sorts ascending.

SORT works in Excel for Microsoft 365, Excel 2021 and Excel for the web.

The Sort button or a formula

The Sort button on the Data tab rearranges the rows in place. A SORT formula returns a sorted copy and leaves the source as it was. The copy updates when the source changes, so you don’t have to sort again.

A formula is useful when several places need the same order, or when you want to keep the original order visible.

Sorting by a column you don’t show

SORT can only sort by a column inside the range it returns. To show columns A and B but sort by column C, use SORTBY, which takes the sort range as its own argument:

=SORTBY(A2:B20, C2:C20, -1)

The sort range must have the same number of rows as the range being returned.

Sorting by more than one column

SORTBY accepts more pairs of a sort range and a sort order. This formula sorts by region from A to Z, then by sales from largest to smallest within each region:

=SORTBY(A2:C20, B2:B20, 1, C2:C20, -1)

Numbers stored as text

If numbers are stored as text, they sort alphabetically, so 10 comes before 2. Text that looks like numbers is usually left-aligned in its cells. Convert those values to real numbers first, using the steps in how to convert text to numbers in excel, and the sort will follow the numeric order.

Removing duplicates and sorting in one step

If you need an excel formula to remove duplicates and sort the list at the same time, nest UNIQUE inside SORT:

=SORT(UNIQUE(A2:A100))

Leave room for the result

SORT output spills like other dynamic array formulas. Keep the cells below and to the right of the formula empty. If something is in the way, the formula shows #SPILL!.

Choosing the range

  • If row 1 holds headers, start the range at row 2. Otherwise the headers get sorted into the data.
  • A range that ends at row 20 won’t include new rows added at row 21. If the list grows, use a larger range, such as A2:C500.
  • Blank rows inside the range come back in the result as blanks, so keep an eye on them when you use a large range.

Google Sheets

Google Sheets also has SORT, but its arguments are different. The sort column is a number, and the direction is TRUE for ascending or FALSE for descending:

=SORT(A2:C20, 3, FALSE)

The result is the same as the Excel example above, sorted by the third column from largest to smallest.

The complete guide to dynamic arrays

What Dynamic Array Formulas Are in Excel
Read the guide

Keep exploring

Guides from other topics

All topics →