Lookup & reference function

SORTBY function

Sorts a range by the values in one or more other ranges, which do not have to be part of the output.

Available in Microsoft 365, Excel 2021 and later, Excel for the web, Excel for Mac 2021+

Excel 2021 and laterNot in Google Sheets

Syntax

SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
ArgumentWhat it means
arrayRequiredThe range or array to return in sorted order.
by_array1RequiredThe range or array to sort by. It must be the same height as array (or the same width when sorting columns).
sort_order1Optional1 ascending (default) or -1 descending.
by_array2, sort_order2, ...OptionalFurther sort keys, applied in order to break ties.

Returns: A sorted copy of array that spills.

How SORTBY works

SORTBY is the flexible sibling of SORT. Instead of naming a column position inside the output, you hand it the actual range to sort by. That means you can sort a list of names by revenue without showing the revenue column, and you can sort by a calculated array - such as a custom priority order - that does not exist anywhere on the sheet.

Arguments come in pairs: a range to sort by and its direction. The first pair is the main sort, the second breaks ties, and so on. Each by_array must have the same number of rows as the data. Because the key can be a formula, you can sort by text length, by a MATCH position against a custom list (High, Medium, Low), or by a random number from RANDARRAY to shuffle a list.

Examples

1Names ordered by a hidden column

G2fx=SORTBY(A2:A8, E2:E8, -1)
ABCDEFG
1RepRegionProductUnitsRevenue
2AnaNorthLaptop43600Dev
3BenSouthMonitor101490Ana
4ChloeNorthMonitor6894Ana
5AnaEastLaptop21800Ben
6DevSouthLaptop54500Chloe
7BenEastMouse30360Ben
8ChloeNorthMouse12144Chloe

Only the Rep names are returned, but they are ordered by Revenue in column E, highest first.

2Custom order: High, Medium, Low

D2fx=SORTBY(A2:B6, MATCH(B2:B6, {"High","Medium","Low"}, 0), 1, A2:A6, 1)
ABCDE
1TaskPriority
2Update price listLowCall supplierHigh
3Fix invoice errorHighFix invoice errorHigh
4Order stockMediumOrder stockMedium
5Call supplierHighArchive emailsLow
6Archive emailsLowUpdate price listLow

MATCH turns each priority into 1, 2 or 3, which sorts High first. The second key (the task name, A-Z) breaks ties within each priority.

3Two keys from the data

G2fx=SORTBY(A2:E8, B2:B8, 1, E2:E8, -1)
ABCDEFGHIJK
1RepRegionProductUnitsRevenue
2AnaNorthLaptop43600AnaEastLaptop21800
3BenSouthMonitor101490BenEastMouse30360
4ChloeNorthMonitor6894AnaNorthLaptop43600
5AnaEastLaptop21800ChloeNorthMonitor6894
6DevSouthLaptop54500ChloeNorthMouse12144
7BenEastMouse30360DevSouthLaptop54500
8ChloeNorthMouse12144BenSouthMonitor101490

Region A-Z, then Revenue largest first within each region - the same as SORT with {2,5} but using ranges.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 18 KB

Common errors and how to fix them

ErrorWhy it happensFix
#VALUE!A by_array is a different size from array, or it is not a single row or column.Make every sort range the same height as the data.
#SPILL!The output area is blocked.Clear the cells below/right of the formula.

Tips

  • Shuffle a list: =SORTBY(A2:A20, RANDARRAY(ROWS(A2:A20))).
  • Sort by text length: =SORTBY(A2:A20, LEN(A2:A20), -1).
  • Add a final tie-breaker (such as the name) so the order is always predictable.

Google Sheets

Use SORT with an outside range as the sort column, for example SORT(A2:B10, C2:C10, FALSE).

SORTBY in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
DanishSORTER.EFTER
DutchSORTEREN.OP
FinnishLAJITTELE.ARVOJEN.PERUSTEELLA
FrenchTRIERPAR
GermanSORTIERENNACH
HungarianRENDEZÉS.ALAP.SZERINT
ItalianDATI.ORDINA.PER
PolishSORTUJ.WEDŁUG
Portuguese (Brazil)CLASSIFICARPOR
Portuguese (Portugal)ORDENARPOR
RussianСОРТПО
SpanishORDENARPOR
SwedishSORTERAEFTER
TurkishSIRALAÖLÇÜT

Recipes that use SORTBY