SORTBY
Short answer
Rank anything by any column
SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
- Category
- Dynamic Arrays & Modern Excel
- Excel version
- 2021+
- Also searched as
- sort by another column, top 10 customers, largest variances first, multi-key sort, sort by absolute value
What it returns
SORTBY returns array reordered by the values in one or more other arrays. The output has the same shape as array. The arrays you sort by can be columns that sit in the source, columns you do not include in the output, or arrays you compute inside the formula.
That last point is what separates it from SORT. SORT can only order by a column of the array it returns. SORTBY can order a variance report by ABS(variance) while showing the signed variance, so a $400k overspend and a $400k underspend rank next to each other at the top.
The source stays where it is. SORTBY writes a sorted copy as a spill, which matters in a model that gets reviewed: nobody has to ask whether the ledger was re-sorted before the numbers were pulled.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
array | Yes | The rows to return. Var[[Account]:[Variance]]. |
by_array1 | Yes | The key to sort by. One column (or one row) the same height as array. Can be computed: ABS(Var[Variance]). |
sort_order1 | No | 1 ascending (default). -1 descending. |
| further pairs | No | Tie-breakers, applied in order. by_array2 sorts rows that tie on by_array1. |
Two rules cause most of the errors. Every by_array must be a single column or row, and it must match array in height. A Table column next to a fixed range one row longer gives #VALUE!.
The sort order defaults to ascending, which is rarely what a finance ranking wants. Write -1 every time you mean “largest first”. It is the argument people forget.
In a financial model
SORTBY lives on output and analysis tabs. The source is a Table of computed figures, and SORTBY presents it in the order a reader needs: biggest variances first, top customers first, overdue invoices oldest first.
It pairs with TAKE for the top-N table that every monthly pack carries. SORTBY ranks, TAKE keeps the first ten rows, and the result updates when the actuals land. No one re-sorts, no one retypes, and no one gets the ranking wrong because they sorted before the last journal posted. A top-customer table usually wants each customer’s share of revenue beside the figure. PERCENTOF as GROUPBY’s function argument returns both, and SORTBY or GROUPBY’s own sort puts them in order.
Keep the variance arithmetic upstream. The play on nothing calculated on an output tab argues for computing variance in the calculation layer, as a column in a Table, and letting the output tab only arrange it. A sort key like ABS(Var[Variance]) is presentation logic, so it can sit inside the SORTBY.
Multi-key sorts replace the helper column that used to concatenate two keys. Region ascending, then revenue descending, is two pairs of arguments.
Worked example
A budget-versus-actual Table Var on tab Calc, with columns Account, Owner, Budget, Actual, Variance. Variance is actual minus budget, so overspends are positive on cost lines.
The ten largest variances in either direction, showing the signed figure:
=TAKE(SORTBY(Var[[Account]:[Variance]], ABS(Var[Variance]), -1), 10)
| Source: Account | Source: Variance | Output: Account | Output: Variance |
|---|---|---|---|
| Travel | 18,000 | Contractors | -412,000 |
| Contractors | -412,000 | Software | 385,000 |
| Software | 385,000 | Rent | 96,000 |
| Rent | 96,000 | Travel | 18,000 |
| Training | -2,500 | Training | -2,500 |
Owner, Budget and Actual are left out of the illustration. The underspend on contractors ranks first even though it is the smallest number in the column. A plain descending sort on Variance would have put it last.
A two-key sort, owner then variance largest first, for a pack where each budget holder reads their own block:
=SORTBY(Var[[Account]:[Variance]], Var[Owner], 1, ABS(Var[Variance]), -1)
If the pack shows only Account, Owner and Variance, drop the other columns with CHOOSECOLS inside the formula. The sort keys still work because they reference the source Table, not the output.
Old way vs new way
Before dynamic arrays, a top-ten list meant a helper column of absolute variances and a LARGE and INDEX formula filled down ten rows:
=INDEX(Var[Account], MATCH(LARGE(Var[AbsVar], ROWS($1:1)), Var[AbsVar], 0))
The new version:
=TAKE(SORTBY(Var[[Account]:[Variance]], ABS(Var[Variance]), -1), 10)
The old version returns the same account twice when two variances tie, because MATCH always finds the first. It also needs one formula per column. SORTBY returns every column of each row together, ties included, with no helper column.
Where it breaks
SORTBY returns #VALUE!. A by_array is a different height from array, or it is two columns wide. This happens when one argument is a Table column and the other a fixed range, or the sort key is a whole column. Fix: build every argument from the same Table so they stay the same height.
The largest values come out at the bottom. sort_order was left out, and it defaults to ascending. Fix: write -1 for descending.
Numbers sort in the wrong order: 1,000 before 200. The key column holds numbers stored as text, which sort as text. Fix: correct the type where the data lands, or sort by --Var[Key] until the source is fixed.
The header row sorts into the middle of the data. array was a plain range that included the header. Fix: point SORTBY at the Table’s data body, Var[[Account]:[Variance]], and put the headers above the spill as static labels.
SORTBY returns #SPILL!. The output needs more rows than the space below it allows, usually because the source grew. Fix: clear the cells below, or cap the output with TAKE so its size is fixed.
SORTBY returns #NAME?. The workbook opened in Excel 2019 or earlier. Fix: if the file must travel, sort a pasted copy for the version that ships, and keep the formula in the working model.
Related functions
- TAKE. Keep the first N rows of the sorted result. Together they are the top-ten table.
- FILTER. Narrow the rows before ranking: only cost lines, only accounts over a materiality threshold.
- UNIQUE. Rank a distinct list, such as vendors ordered by total spend.
- GROUPBY. Aggregates and sorts in one formula. Reach for it when the ranking is over totals you have not computed yet.
- CHOOSECOLS. Return only the columns the reader needs, while the sort keys keep reading the full source.
- LET. Name the source and the key once when the formula gets long, per LET instead of repeating a fragment.
- SORT. When the key is a column of the output itself and no computed key is needed.
