Site icon Best Excel Tutorial

How to Use the SORT and SORTBY Functions in Excel

Excel’s ribbon Sort tool rearranges your original table in place, and you have to re-run it every time your numbers change. The 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.

  1. Add headers: Copy cells A1:C1 into E1:G1.
  2. Select the top-left output cell: Click cell E2.
  3. Enter the SORT formula: Specify column 3 (Sales) as the sort_index and -1 for descending order:
    =SORT(A2:C7, 3, -1)
  4. 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.

  1. Click an empty cell with room to spill (for example, I2) after pasting your headers into I1:K1.
  2. Type the SORTBY formula with two sort criteria pairs:
    =SORTBY(A2:C7, B2:B7, 1, C2:C7, -1)
  3. 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.

  1. Click cell M2.
  2. Pass only the Rep column (A2:A7) as the first argument, and the Sales column (C2:C7) as the by_array1 argument:
    =SORTBY(A2:A7, C2:C7, -1)
  3. 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_index number is larger than the number of columns in your array (for example, entering 4 for a 3-column table).
  • #VALUE! error in SORTBY: The by_array range has a different number of rows than the main array (such as A2:C7 paired with C2: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.
Exit mobile version