SORT and SORTBY in Excel: Sort Tables Dynamically by Multiple Columns

Isometric spreadsheet illustration showing SORTBY in Excel aligning data bars into sorted order

Using Excel’s ribbon Sort button works fine for static snapshots, but the moment new sales numbers arrive, you have to re-sort the table manually. With SORT and SORTBY in Excel, you can create live, formula-driven leaderboards and multi-column reports that re-order themselves automatically whenever source values change.

Availability & Compatibility: Per the official SORT function documentation and SORTBY function documentation, both functions are available in Excel for Microsoft 365 (Windows and Mac), Excel for the web, Excel 2024, and Excel 2021 (as well as Google Sheets). Older perpetual versions such as Excel 2019 and Excel 2016 do not support them and will display #NAME?. Also note that linking dynamic array formulas across separate workbooks requires both workbooks to stay open—closing the source workbook causes #REF! errors on refresh.

How SORT Works (Single-Column & Index Sorting)

SORT sorts the contents of a range or array by a column (or row) index number and spills the sorted result into adjacent cells. Here is the syntax:

=SORT(array, [sort_index], [sort_order], [by_col])
  • array (Required): The range or dynamic array you want to sort.
  • [sort_index] (Optional): A number indicating which column (or row) to sort by. Defaults to 1.
  • [sort_order] (Optional): Use 1 for ascending order (default) or -1 for descending order.
  • [by_col] (Optional): Set to FALSE (or omit) to sort by rows, or TRUE to sort horizontally across columns.

Suppose your Practice sheet tracks eight reps in A6:D13 (Sales Rep, Region, Q4 Revenue, and Deals Closed). To build a live top-to-bottom revenue leaderboard in cell F6, enter:

=SORT(A6:D13, 3, -1)

Because Q4 Revenue sits in column 3 and we passed -1, Excel spills Liam Chen ($142,000) at the top and Lucas Meyer ($76,000) at the bottom.

How SORTBY in Excel Works (Multi-Column & Flexible Sorting)

While SORT relies on a hardcoded column number (which breaks if someone inserts a new column inside your source table), SORTBY in Excel sorts an array based on one or more corresponding ranges—even if those sort columns aren’t included in the final output:

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

To sort our rep table alphabetically by Region ascending (1), and then by Q4 Revenue descending (-1) within each region, enter:

=SORTBY(A6:D13, B6:B13, 1, C6:C13, -1)

Need to display only the rep names ranked by Deals Closed without showing the Deals column? Pass A6:A13 as array and D6:D13 as by_array1: =SORTBY(A6:A13, D6:D13, -1). You can even randomize any list dynamically by pairing SORTBY with RANDARRAY and COUNTA: =SORTBY(A6:A13, RANDARRAY(COUNTA(A6:A13))), or wrap the FILTER function in Excel inside SORTBY to sort only qualifying records.

SORT vs. SORTBY: Which One Should You Use?

Per Microsoft Support, SORTBY is generally more flexible for grid data than SORT. Compare both functions below:

FeatureSORTSORTBY
How Sort Column Is SpecifiedColumn index number (3)Range reference (C6:C13)
Resilient to Column Insertions?No (index shifts)Yes (range updates automatically)
Multi-Level SortingSingle index (unless array constant)Multiple by_array pairs natively
Sort by Unreturned Column?NoYes
CompatibilityExcel 2021+, M365, SheetsExcel 2021+, M365, Sheets

Troubleshooting #VALUE!, #REF!, and #SPILL! Errors

  • #VALUE! error: Per Microsoft Support, SORTBY returns #VALUE! if any by_array argument is more than one row high or one column wide, if the by_array dimensions don’t match array, or if sort_order is anything other than 1 or -1. Check our guide on how to fix the #VALUE! error in Excel.
  • #REF! error on external links: If your SORT or SORTBY formula references another workbook and you close that source workbook, Excel returns #REF! when refreshing—see how to repair #REF! errors in Excel.
  • #SPILL! error: Clear any text, totals, or merged cells inside the output spill range.
  • #NAME? error: The workbook was opened in Excel 2019 or earlier, where SORT and SORTBY are unavailable.

Frequently Asked Questions

Download the practice workbook

The same data as this tutorial, with empty answer cells and a Solutions sheet.

Team Xl in Excel

Team Xl in Excel

Spreadsheet Architect & Educator at Xl in Excel. Sharing advanced formulas, dynamic arrays, and clean data modeling techniques.

Learn More About Team Xl in Excel →

Leave a Reply

Your email address will not be published. Required fields are marked *

Zoom: 100% | UTF-8 | Xl in Excel