ExcelReady
CourseFunction guidesBlog
Start freeStart learning for free
ExcelReady

Be Excel-ready before the interview. Hands-on practice in your browser, with instant feedback.

Learn

  • Course
  • Function guides
  • Blog

Product

  • Certificate

Company

  • Editorial policy
  • Contact
  • Terms
  • Privacy

© 2026 ExcelReady. All rights reserved.

  1. Blog
  2. Dynamic arrays
  3. SORT and SORTBY in Excel

SORT and SORTBY in Excel

By ExcelReady Team·Updated October 30, 2026·6 min read

The short answer

SORT returns a sorted copy of a range: =SORT(A2:C9,3,-1) sorts by the 3rd column, largest first. SORTBY sorts one range by the values in another, so the column you sort by doesn't have to be in the result, and it handles several sort levels. Both leave the original data unchanged and need Excel 2021, Excel 2024 or Microsoft 365.

Key takeaways

  • =SORT(array, [sort_index], [sort_order]) sorts by a column number counted inside the range.
  • sort_order is 1 for smallest first (A to Z) or -1 for largest first.
  • =SORTBY(array, by_array, [sort_order], ...) sorts by another range, which doesn't have to be in the result.
  • Use SORTBY with two by_arrays to sort by one column, then another.
  • Both spill their results, leave the source data as it is and update when it changes.
On this page+
  1. How the Excel SORT function works
  2. How to sort by another column, largest first
  3. How SORTBY works
  4. How to sort by two columns
  5. SORT vs SORTBY: which should you use?
  6. How to combine SORT with FILTER and UNIQUE
  7. Common problems with SORT and SORTBY
  8. Practise it

The Excel SORT function returns a sorted copy of a range, and SORTBY sorts a range by the values in another one. Unlike Data > Sort, both are formulas: the original data stays as it is, and the sorted copy updates when the data changes. This guide is for anyone building leaderboards, top-seller lists or tidy reports. SORT and SORTBY need Excel 2021, Excel 2024 or Microsoft 365.

How the Excel SORT function works#

SORT takes a range and returns it in order. With only the range, it sorts by the first column, A to Z.

Excel formula
=SORT(array, [sort_index], [sort_order], [by_col])
  • array is the data to sort, without its header row.
  • sort_index is which column of the array to sort by, counted from the array's left edge. The default is 1.
  • sort_order is 1 for smallest first (A to Z, the default) or -1 for largest first (Z to A).
  • by_col is optional and rarely needed: TRUE sorts columns left to right instead of rows.

Like FILTER and UNIQUE, SORT is a dynamic array function: you type it in one cell and the results spill into the cells around it. If those cells aren't empty, you see #SPILL!; see our guide to the #SPILL! error.

Here is a product list:

ABC
1ProductCategoryUnits
2Trail MixSnacks340
3Cold BrewDrinks512
4Granola BarSnacks275
5Sparkling WaterDrinks610
6Dark ChocolateSnacks198
7Green TeaDrinks233
8Rice CrackersSnacks421
9Oat MilkDrinks389

In E2, =SORT(A2:C9) returns all eight rows in alphabetical order by product, from Cold Brew to Trail Mix, with each product's category and units beside it.

How to sort by another column, largest first#

To put the best sellers at the top, sort by Units, the 3rd column of the range, with -1:

Excel formula
=SORT(A2:C9,3,-1)

The result starts with Sparkling Water (610), Cold Brew (512) and Rice Crackers (421), and ends with Dark Chocolate (198).

The same -1 works for text. =SORT(A2:A9,1,-1) lists the product names from Z to A, starting with Trail Mix and ending with Cold Brew.

sort_index counts columns inside the range, not on the sheet. If you sort only B2:C9, Units is column C on the sheet but the 2nd column of the range, so the formula is =SORT(B2:C9,2,-1).

How SORTBY works#

SORTBY sorts one range by the values in another range of the same height.

Excel formula
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
  • array is what you want to see.
  • by_array1 is what decides the order.
  • sort_order1 is 1 or -1, as in SORT.

The big difference is that the column you sort by doesn't have to be in the result. To list only the product names, best sellers first:

Excel formula
=SORTBY(A2:A9,C2:C9,-1)

This returns Sparkling Water, Cold Brew, Rice Crackers, Oat Milk, Trail Mix, Granola Bar, Green Tea, Dark Chocolate, without showing the units. With SORT, the units column would have to be part of the range.

How to sort by two columns#

SORTBY takes as many by_array and sort_order pairs as you need. This sorts by category A to Z, then by units from highest to lowest inside each category:

Excel formula
=SORTBY(A2:C9,B2:B9,1,C2:C9,-1)

The Drinks come first (Sparkling Water 610, Cold Brew 512, Oat Milk 389, Green Tea 233), then the Snacks (Rice Crackers 421, Trail Mix 340, Granola Bar 275, Dark Chocolate 198). This is the same as a two-level sort in Data > Sort, but it stays up to date. If a product's units change, it moves to its new place by itself, which is exactly what you want for a leaderboard or a weekly top-sellers report. Add a third pair to break any remaining ties, such as by product name.

SORT vs SORTBY: which should you use?#

Use SORT for a quick sort by one column of the data you're returning. Use SORTBY when the sort column isn't in the result, or when you need more than one sort level.

TaskSORTSORTBY
Sort a table by one of its columns=SORT(A2:C9,3,-1)=SORTBY(A2:C9,C2:C9,-1)
Sort by a column you don't showNot directly=SORTBY(A2:A9,C2:C9,-1)
Sort by two or more columnsPossible, but harder to read=SORTBY(A2:C9,B2:B9,1,C2:C9,-1)
How you name the sort columnA number (3)A range (C2:C9)

A range is also safer than a number: if someone inserts a column, SORTBY's range moves with it, while SORT's 3 stays a 3. The same idea is behind lookups that use MATCH or XMATCH instead of typed column numbers; see MATCH vs XMATCH.

How to combine SORT with FILTER and UNIQUE#

SORT and SORTBY are often the outer layer of a longer formula. Excel works out the inside function first, then sorts its result.

  • Sorted filter results: =SORT(FILTER(A2:C9,B2:B9="Snacks"),3,-1) shows only the Snacks, best sellers first: Rice Crackers, Trail Mix, Granola Bar, Dark Chocolate. See the FILTER function for the conditions, or the FILTER function guide for a quick reference.
  • A sorted list of categories: =SORT(UNIQUE(B2:B9)) returns Drinks, Snacks, each once. See the UNIQUE function.
  • The top item only: =INDEX(SORT(A2:C9,3,-1),1,1) returns Sparkling Water, row 1 and column 1 of the sorted table.
  • Count the sorted list: if your SORTBY spills from E2, =ROWS(E2#) counts its rows. The # is a spill reference: it means "everything E2 spilled", and it grows with the list.

Common problems with SORT and SORTBY#

Most problems come from the spill, the arguments or the data:

  1. #SPILL!: something is in the way of the result. Clear the cells below and to the right, or move the formula.
  2. #VALUE! from sort_order. It must be exactly 1 or -1. A 0 or 2 returns #VALUE!.
  3. Counting sort_index from column A of the sheet. Count from the first column of your range.
  4. Ranges of different heights in SORTBY. =SORTBY(A2:A9,C2:C10,-1) returns #VALUE!. Make them match.
  5. Numbers stored as text. Text numbers sort as text, so "1000" can land before "250". Convert them to real numbers first.
  6. Including the header row. =SORT(A1:C9) sorts "Product" in with the data. Start on row 2 and type the headers above the result.
  7. #NAME? in older Excel. Excel 2019 and earlier don't have these functions; use Data > Sort there.

Practise it#

Try the SORTBY exercise on this page: the names come out best seller first, without the units column. Then practise SORT in a live spreadsheet and continue with modern Excel functions such as SORTBY, XMATCH and spill references.

Try it yourself

This is a live spreadsheet. Type the formula in the green cell and press Enter.

In E2, list the product names from A2:A9, sorted by the units in C2:C9 from highest to lowest. Use SORTBY.

fx
ABCDEF
1
Product
Category
Units
Best sellers
2
Trail Mix
Snacks
340
3
Cold Brew
Drinks
512
4
Granola Bar
Snacks
275
5
Sparkling Water
Drinks
610
6
Dark Chocolate
Snacks
198
7
Green Tea
Drinks
233
8
Rice Crackers
Snacks
421
9
Oat Milk
Drinks
389
10
11
12

Tip: The array is the names, the by_array is the units, and -1 puts the highest first.

Practise this in a lesson

LessonSORT: order data with a formulaSort a table by any column, ascending or descending, and combine SORT with FILTER and UNIQUE.LessonModern Excel: XMATCH, SORTBY, TEXTSPLIT and spill references (A2#)Split names with TEXTSPLIT, find positions with XMATCH, sort by another column with SORTBY, and refer to a whole spilled result with A2#.Function guideFILTERReturn every row that matches a condition with Excel's FILTER. Multiple criteria, empty results and #SPILL! explained, with live practice.

Frequently asked questions

What is the difference between SORT and SORTBY?

+

SORT sorts by a column number inside the range you return. SORTBY sorts by a separate range, so you can sort by a column you don't show and add several sort levels easily.

How do I sort from largest to smallest with SORT?

+

Use -1 as the third argument. For example, =SORT(A2:C9,3,-1) sorts by the 3rd column with the largest value first.

Does the SORT function change my original data?

+

No. SORT and SORTBY return a sorted copy somewhere else on the sheet. The source stays in its original order, and the copy updates when the source changes.

How do I sort by two columns with a formula?

+

Use SORTBY with two pairs: =SORTBY(A2:C9,B2:B9,1,C2:C9,-1) sorts by column B from A to Z, then by column C from largest to smallest within each group.

Why does SORT show #NAME?

+

Your version of Excel doesn't have it. SORT and SORTBY need Excel 2021, Excel 2024, Microsoft 365 or Excel for the web. In older versions, use Data > Sort instead.

Sources

  • SORT function - Microsoft Support
  • SORTBY function - Microsoft Support

Written and checked by the ExcelReady Team. First published October 30, 2026. The practice exercise runs in the same formula engine as our lessons. Found a mistake? Read our editorial policy or email hello@excelready.app.

On this page

  1. How the Excel SORT function works
  2. How to sort by another column, largest first
  3. How SORTBY works
  4. How to sort by two columns
  5. SORT vs SORTBY: which should you use?
  6. How to combine SORT with FILTER and UNIQUE
  7. Common problems with SORT and SORTBY
  8. Practise it

Keep reading

Dynamic arrays· 5 min read

#SPILL! Error in Excel: What It Means and How to Fix It

#SPILL! means a formula that returns several results can't place them, usually because a cell in the way isn't empty. Click the formula, then the warning icon, to see the reason. Clear or move whatever blocks the spill range, unmerge merged cells, move the formula out of an Excel table, or use a smaller range instead of a whole column.

Dynamic arrays· 7 min read

The UNIQUE Function in Excel

The UNIQUE function returns each different value in a range once, in the order it first appears: =UNIQUE(C2:C9). Type it in one cell and the results spill into the cells below. Wrap it in COUNTA to count distinct values, or in SORT to put them in order. UNIQUE needs Excel 2021, Excel 2024 or Microsoft 365.

Dynamic arrays· 6 min read

The FILTER Function in Excel (With AND and OR)

FILTER returns every row of a range that passes a test: =FILTER(A2:C9,B2:B9="East"). The matching rows spill into the cells below and update when the data changes. For AND, multiply tests in brackets; for OR, add them. The third argument shows a message when nothing matches. FILTER needs Excel 2021, Excel 2024 or Microsoft 365.

Don't just read about Excel. Do it.

Practise real formulas in a live spreadsheet with instant feedback. The first lessons are free, with no download.

Start learning for free