Excel Lesson 22 – SORT function | Dataplexa
Lesson 22 · Advanced Practical

SORT and SORTBY

Sorting in Excel used to mean physically rearranging your data — a destructive, manual operation that you had to redo every time numbers changed. SORT and SORTBY changed that completely. Both functions return a sorted copy of your data as a dynamic spill result, leaving the source untouched. SORT handles the common case of sorting by a column within the returned range. SORTBY is more powerful — it can sort by any array, including columns that are not part of the output at all. Together they are the foundation of every leaderboard, ranked table, and automatically ordered report in modern Excel.

SORT — Sort by a Column in the Return Range

SORT takes a range and returns it sorted. You specify which column to sort by using a column index number — column 1 being the first column in the array you passed in, not column A of the sheet.

=SORT(array, [sort_index], [sort_order], [by_col])

  array       →  The range to sort
  sort_index  →  Which column number to sort by (default: 1)
  sort_order  →  1 = ascending A→Z or smallest first (default)
               -1 = descending Z→A or largest first
  by_col      →  FALSE = sort rows top-to-bottom (default)
                 TRUE  = sort columns left-to-right

=SORT(A2:D10)            →  Sort by column 1, ascending
=SORT(A2:D10, 4, -1)     →  Sort by column 4, descending (largest first)
=SORT(A2:D10, 2)         →  Sort by column 2, A to Z
SORT(A2:D10, 4, -1) → entire table sorted by sales column, highest first
SORT — Leaderboard Sorted by Sales Descending
Source — entry order
Rep Region Sales
SarahNorth£93,000
DavidNorth£114,000
MariaSouth£82,000
PriyaNorth£119,000
JamesEast£117,000
=SORT(A2:C6, 3, -1)
Rep Region Sales ↓
🥇 PriyaNorth£119,000
🥈 JamesEast£117,000
🥉 DavidNorth£114,000
SarahNorth£93,000
MariaSouth£82,000

SORT by Multiple Columns

You can sort by more than one column by passing arrays as the sort_index and sort_order arguments. Excel sorts by the first criterion, then uses the second to break ties, and so on. Wrap the values in curly braces to pass multiple at once.

Sort by Region A-Z, then by Sales descending within each region:
=SORT(A2:C10, {2, 3}, {1, -1})

  {2, 3}   →  Sort by column 2 first, then column 3
  {1, -1}  →  Column 2 ascending, column 3 descending

Sort by Quarter then by Rep name:
=SORT(A2:D20, {2, 1}, {1, 1})
Multi-Column Sort — Region A→Z, then Sales Highest First
Rep Region ↑ Sales ↓
James East £117,000
Priya North £119,000
David North £114,000
Sarah North £93,000
Maria South £82,000
East → North → South alphabetically · Within North: £119K → £114K → £93K

SORTBY — Sort by Any Array

SORT can only sort by columns that are part of the array being returned. SORTBY removes that restriction entirely. You pass the array to return and then separate sort key arrays — and those sort key arrays do not need to be in the returned range at all. This makes SORTBY indispensable when you want to sort output by a computed value or a column you do not want to include in the result.

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

  array      →  The range to return
  by_array1  →  The array to sort by (any range or calculated array)
  sort_order →  1 = ascending (default),  -1 = descending

Return only Rep names, sorted by their Sales (Sales column not in output):
=SORTBY(A2:A6, C2:C6, -1)

Return names and regions, sorted by Sales descending:
=SORTBY(A2:B6, C2:C6, -1)
SORTBY vs SORT — The Key Difference
SORT — sort key must be in the array
=SORT(A2:C6, 3, -1)
Returns 3 columns (A, B, C) sorted by column C (index 3). Column C must be included in the returned range.
❌ Cannot sort by a column you want to exclude from the output
SORTBY — sort key is independent
=SORTBY(A2:B6, C2:C6, -1)
Returns only 2 columns (A, B) sorted by column C — which is NOT in the output. The sort key is completely separate from the returned data.
✅ Sort by any column, computed value, or external array

A practical example: you want to show a name-and-department list sorted by salary, but you do not want the salary column to appear in the output. SORT cannot do this — the sort column must be in the returned array. SORTBY handles it naturally: return A2:B20 (name and department), sort by D2:D20 (salary), and salary never appears in the spill result.

SORTBY With Multiple Sort Keys

SORTBY supports multiple by_array / sort_order pairs, making it ideal for hierarchical sorting — sort by department first, then by salary within each department, using columns that may or may not appear in the output.

Employee data: A=Name, B=Department, C=Start Date, D=Salary

Show Name and Department, sorted by Department A-Z
then by Salary descending within each department:
=SORTBY(A2:B20, B2:B20, 1, D2:D20, -1)

Show Name only, sorted by Start Date (oldest first):
=SORTBY(A2:A20, C2:C20, 1)

Show Name and Salary, sorted by Department then Start Date:
=SORTBY(A2:A20, B2:B20, 1, C2:C20, 1)
SORTBY — Dept A→Z, then Salary Highest First Within Dept
Name Department ↑ Salary ↓ within dept
James Finance £67,000
Maria Finance £61,000
Priya Marketing £55,000
David Marketing £52,000
Sarah Operations £48,000
=SORTBY(A2:C6, B2:B6, 1, D2:D6, -1) — Salary column D not shown in output

SORTBY With Calculated Sort Keys

Because by_array can be any array — not just a column from the sheet — you can sort by a calculated value. This is one of the most powerful and underused features of SORTBY. Sort by absolute variance from a target, by percentage, by a formula result, or by a transformed version of a column.

Sort by absolute distance from a target of £100,000
(closest to target first, regardless of over or under):
=SORTBY(A2:C6, ABS(C2:C6 - 100000), 1)

Sort by percentage of target achieved:
=SORTBY(A2:C6, C2:C6 / 100000, -1)

Sort names alphabetically by last name
(where A2:A6 contains "First Last" format):
=SORTBY(A2:A6, TEXTAFTER(A2:A6, " "), 1)
SORTBY by ABS distance from £100K → David £114K (£14K away), Sarah £93K (£7K away)... actually Sarah first

The SORTBY(A2:A6, TEXTAFTER(A2:A6, " "), 1) example is a classic real-world use case — sorting a list of full names by last name, when the last name is the part after the space. No helper column needed, no manual splitting, just a calculated sort key passed directly into SORTBY.

Combining SORT and SORTBY With FILTER

In most real dashboards and reports, you want to filter and sort together. SORT and SORTBY nest cleanly inside or around FILTER — producing filtered, sorted output from a single formula.

North region rows, sorted by sales highest first:
=SORT(FILTER(A2:C20, B2:B20="North"), 3, -1)

North region rows, sorted by start date oldest first
(date not in returned columns):
=SORTBY(FILTER(A2:C20, B2:B20="North"), FILTER(D2:D20, B2:B20="North"), 1)

Unique regions sorted alphabetically:
=SORT(UNIQUE(B2:B20))
SORT(FILTER(North rows), 3, -1) → North reps in order: Priya £119K, David £114K, Sarah £93K

The SORTBY(FILTER(...), FILTER(...)) pattern is the key to sorting filtered output by a column that is not in the result. Both FILTER calls use the same condition, so the sort key array aligns row-for-row with the returned data. This combination is used extensively in professional dashboards.

SORT vs SORTBY — When to Use Each

Scenario Use SORT Use SORTBY
Sort by a column that is in the output ✅ Simpler ✅ Works too
Sort by a column NOT in the output ❌ Not possible ✅ Designed for this
Sort by a calculated / transformed value ✅ Pass formula as by_array
Sort by multiple columns ✅ {col, col}, {order, order} ✅ Pairs of by_array, order
Sort columns left-to-right (by_col) ✅ by_col = TRUE ❌ Rows only
💡 Teacher's Note
A question I hear a lot: "why does my sorted table not update when I add new data?" The answer is almost always that the array reference in SORT or SORTBY is a fixed range like A2:C10 — so new rows added to A11 onwards are not included. The fix is to either extend the range generously (A2:C1000), or better yet, format the source data as an Excel Table and reference the table columns (SalesData[Rep], SalesData[Sales]). Table references expand automatically when new rows are added, so the sorted output always includes everything.

🟠 Practice

Q1. Sales data is in A2:C20. Column C contains sales figures. Write a SORT formula that returns all three columns sorted by column C in descending order.




Q2. You want to return only columns A and B (name and department) from A2:D20, sorted by column D (salary) in descending order — but D should not appear in the output. Which function do you use, and write the formula.




Q3. Write a formula that returns all rows from A2:C20 where column B equals "Finance", sorted by column C descending.



🟣 Quiz

Q1. You write =SORT(A2:C10, {2,3}, {1,-1}). What does this do?







Q2. What is the main advantage of SORTBY over SORT?







Q3. You want to sort a list of full names in A2:A20 alphabetically by last name (format is "First Last"). Which formula achieves this without a helper column?






Next up — UNIQUE, where you will learn how to extract deduplicated lists, count distinct values, and build dynamic category headers that update automatically as your data grows.