SORT and SORTBY functions solve this by outputting a live, automatically sorted copy of your table elsewhere on the sheet.Both functions belong to Excel’s dynamic arrays family and are available in Microsoft 365, Excel 2021, Excel 2024 and Excel for the Web.
SORT vs. SORTBY: Which One Should You Use?
- Use SORT for quick sorting when the column you are sorting by is inside the output range and you refer to it by column number (such as column
3). - Use SORTBY when you want multi-level sorting (for example, Region A to Z, then Sales high to low), when you want to sort by a helper column outside the output range, or when you want formulas that will not break if someone inserts a new column.
Function Syntax
=SORT(array, [sort_index], [sort_order], [by_col])
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
In both functions, sort_order uses 1 for ascending (smallest to largest, or A to Z) and -1 for descending (largest to smallest, or Z to A). If omitted, Excel defaults to 1.
Sample Dataset
Enter this table in cells A1:C7:
| Rep | Region | Sales |
|---|---|---|
| Anna | North | 1200 |
| Mark | South | 450 |
| Elena | North | 800 |
| David | East | 950 |
| Sara | North | 1500 |
| Tom | South | 1100 |
Step-by-Step: Sort Highest to Lowest Using SORT
Let us create a live leaderboard ranked by Sales from highest to lowest.
- Add headers: Copy cells
A1:C1intoE1:G1. - Select the top-left output cell: Click cell
E2. - Enter the SORT formula: Specify column
3(Sales) as thesort_indexand-1for descending order:=SORT(A2:C7, 3, -1) - Press Enter: Excel spills the entire table sorted from Sara (1500) down to Mark (450). If Mark’s sales later change to 2000, he jumps to the top immediately.
Step-by-Step: Multi-Level Sorting Using SORTBY
Now let us sort the table first by Region alphabetically (A to Z), and then by Sales from highest to lowest within each region.
- Click an empty cell with room to spill (for example,
I2) after pasting your headers intoI1:K1. - Type the
SORTBYformula with two sort criteria pairs:=SORTBY(A2:C7, B2:B7, 1, C2:C7, -1) - Press
Enter. Excel groups East first, then North (ordered Sara 1500, Anna 1200, Elena 800), then South (ordered Tom 1100, Mark 450).
Step-by-Step: Sort Names by a Column Not Shown in the Result
Suppose you only want to output a single column of Rep names ranked by their Sales, without displaying the Region or Sales columns.
- Click cell
M2. - Pass only the Rep column (
A2:A7) as the first argument, and the Sales column (C2:C7) as theby_array1argument:=SORTBY(A2:A7, C2:C7, -1) - Press
Enter. Only the ranked names spill down column M.
Practical Combo: Get a Sorted Top 3 List
You can combine SORT with the TAKE function to display only the top 3 performers automatically:
=TAKE(SORT(A2:C7, 3, -1), 3)
You can also pair SORTBY with RANDARRAY to shuffle data dynamically. Check our full walkthrough on how to randomize a list in Excel.
Common Errors and Fixes
- #VALUE! error in SORT: Your
sort_indexnumber is larger than the number of columns in your array (for example, entering4for a 3-column table). - #VALUE! error in SORTBY: The
by_arrayrange has a different number of rows than the mainarray(such asA2:C7paired withC2:C8). Ensure all ranges start and end on the exact same rows. - #SPILL! error: Clear any text or values blocking the cells below or to the right of your formula.
