Excel functions · Dynamic arrays

SORTBY function in Excel

SORTBY sorts a range by the values in one or more other ranges, which don't have to be part of the result, such as a list of reps sorted by their order amounts. Each sort level is a range and an order, so multi-level and custom-order sorts are easy to read.

Last reviewed

Syntax

=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
arrayrequired
The range to sort and return.
by_array1required
The range to sort by. Same height as array (or same width when sorting columns).
sort_order1optional
1 ascending (the default) or -1 descending.
by_array2, sort_order2, ...optional
More sort levels, applied in order to break ties.
  • The result spills: type the formula in one cell and it fills as many cells as it needs, and it updates when the data changes.
  • Rows that tie on every sort level keep their original order.

Which versions have SORTBY

Microsoft 365, Excel for the web, and Excel 2021 or later (Windows and Mac). Not in Excel 2019 or 2016.

SORTBY availability by Excel version
VersionHas it
Microsoft 365 (Windows and Mac)Yes
Excel for the webYes
Excel 2024Yes
Excel 2021Yes
Excel 2019No
Excel 2016No

Google Sheets

Google Sheets has no SORTBY. Its SORT accepts a separate range as the sort column, which does the same job: =SORT(B2:B9, E2:E9, FALSE).

Examples

The examples use this sample sheet.

Orders (A1:E9)
RowABCDE
1Order IDRepRegionProductAmount
21001AnaNorthLaptop1200
31002BenSouthMonitor300
41003AnaNorthMonitor350
51004CaraEastLaptop1150
61005BenSouthLaptop1250
71006DevWestKeyboard80
81007CaraEastMonitor320
91008AnaSouthKeyboard90
  1. 1.Sort names by a column you don't return

    =SORTBY(B2:B9, E2:E9, -1)

    Result: Spills Ben, Ana, Cara, Ana, Cara, Ben, Ana, Dev

    Ben
    Ana
    Cara
    Ana
    Cara
    Ben
    Ana
    Dev

    The reps, ordered by the amount of each order from 1,250 down to 80.

  2. 2.Two sort levels

    =SORTBY(A2:A9, C2:C9, 1, E2:E9, -1)

    Result: Spills 1004, 1007, 1001, 1003, 1005, 1002, 1008, 1006

    1004
    1007
    1001
    1003
    1005
    1002
    1008
    1006

    Order IDs by region A–Z, then by amount from largest to smallest within each region.

  3. 3.A custom order

    =SORTBY(A2:A9, XMATCH(C2:C9, {"North","South","East","West"}))

    Result: Spills 1001, 1003, 1002, 1005, 1008, 1004, 1007, 1006

    1001
    1003
    1002
    1005
    1008
    1004
    1007
    1006

    XMATCH turns each region into its place in your list (North 1, South 2…), and SORTBY sorts by that number.

Common errors and fixes

SORTBY errors, causes and fixes
ErrorWhy it happensFix
#VALUE!A by_array isn't the same height as array, or a sort order isn't 1 or -1.Use ranges that cover the same rows, and 1 or -1 for each order.
#SPILL!The result needs more cells than are free: something is in the way, or the formula is inside an Excel table.Clear the cells below and to the right, or move the formula outside the table.
#NAME?Your Excel doesn't have dynamic array functions (Excel 2019 or older).Open the file in Microsoft 365, Excel for the web, or Excel 2021 or later.

Do it with GPTExcel

Skip writing SORTBY by hand

Upload your Excel or CSV file to Chat and ask in plain English. GPTExcel writes the formula for your file and your version of Excel or Google Sheets.

your-file.xlsx

Sort my regions in the order North, South, East, West instead of A to Z.

Done. I used SORTBY on your sheet and checked the results against your data.

your-file-updated.xlsxYour original stays unchanged
  • Written for your platform and version
  • Checked against your actual data
  • Newest functions looked up in official docs
Try it free Formula Generator

Free plan, no credit card.

SORTBY questions

Does Google Sheets have SORTBY?
No. In Google Sheets, use SORT with a separate range as the sort column, such as =SORT(B2:B9, E2:E9, FALSE), which sorts the reps by amount, largest first.
How do I sort by a custom list in Excel with a formula?
Turn each value into its position in your list with XMATCH and sort by that: =SORTBY(A2:A9, XMATCH(C2:C9, {"North","South","East","West"})).

Skip the formula hunt

Upload a spreadsheet and ask in plain English. Free plan, no credit card.

Start free