In Excel: use =SORTBY(array, by_array, order) — it sorts one range by the values in ANOTHER, which SORT cannot do.
Syntax
Arguments
| Argument | Description | |
|---|---|---|
array | required | The range to return, sorted. |
by_array1 | required | The values to sort by. Need not be part of array. |
sort_order1 | optional | 1 ascending (default), -1 descending. |
Related functions
What this does
SORTBY returns a range ordered by a different range. That is its whole reason to exist alongside SORT: the column you want to sort by need not be part of what you return, and it can be a calculated expression that exists nowhere on the sheet. Sorting names by a score you compute on the fly, or ordering a summary by a hidden priority column, are both one formula. Multiple by_array and order pairs give multi-level sorting. Like every dynamic-array function it spills, so it needs empty cells below and returns #SPILL! otherwise. Most people learn this as a sequence of clicks and forget it by next week; learning it as a pattern instead is what lets you apply it to the next, slightly different version of the problem without starting from scratch. That is the difference this page is trying to make. For “sortby function excel”, the reliable version is a short checking loop, not just the first command that appears to work. Run it on a deliberately small range first, watch how the affected formula change, and only then apply the same setup to the full sheet. When a formula is involved, keep the inputs labelled beside it, reference cells instead of typing values, and apply number formatting only after the result checks out. That is what makes a data step that keeps your analysis trustworthy useful in real work: repeatable, auditable, and not dependent on memory or luck.
A worked example
Names in A2:A50 ordered by scores in B2:B50, highest first, returning only the names: =SORTBY(A2:A50, B2:B50, -1). Sorting by a computed key that appears nowhere on the sheet: =SORTBY(A2:A50, LEN(A2:A50), 1) orders by name length. Two levels — region ascending, then revenue descending: =SORTBY(A2:C50, A2:A50, 1, C2:C50, -1). SORTBY sorts by a key that need not exist as a column, which removes the helper column the Sort button would have required. A practical tip before you scale it up: build it once on a small block of test data, confirm the number against the tool on this page, and only then point it at your real sheet. That one habit catches almost every mistake while it is still cheap to fix, long before a wrong figure reaches a report or a colleague.
In Google Sheets
Everything above works in Google Sheets too. Excel and Sheets share the formula syntax used here; only the surrounding menus are arranged differently. That portability is deliberate — learn it once and it follows you between the two tools and across Windows and Mac. Nothing on this page is behind a login: the tool runs entirely in your browser, the formula is shown in full with one-click copy, and the steps work the same on Windows and Mac. That is the whole promise here — the exact answer, a way to prove it on your own numbers, and just enough context to make it stick. If you take one thing from this page on “sortby function excel”, make it the habit rather than the keystrokes: set the problem up with labelled inputs, reference those cells, and let Excel do the recomputing. Bookmark the page for the syntax, but do the example once in a blank sheet and check it against the tool above — five minutes of hands-on practice fixes the method in memory far better than re-reading, and it surfaces the small snags while they are still harmless. After that the technique is genuinely yours: faster than searching for it again, and reliable enough to drop into work that other people depend on.
Common mistakes
- Using SORT when the sort key is not in the returned range — that is exactly what
SORTBYis for. - by_array of a different length to array, which returns
#VALUE!. - Placing it where cells below are occupied, which returns
#SPILL!.
Frequently asked questions
What is the difference between SORT and SORTBY?
SORT orders by a column index within the range itself. SORTBY orders by a separate range or a calculation, which need not be returned at all.
Can I sort by more than one column?
Yes — add further by_array and sort_order pairs, applied in the order given.
Does it change the source data?
No. It returns a sorted copy and leaves the original untouched, unlike the Sort button.