SORTBY
Sorts an array based on one or more aligned sort-by arrays.
SORTBY separates what is returned (array) from what determines ordering (by_array).
The Formualizer engine in this repository at 0.15.0 plus unreleased changes computes the example results. Known differences from Excel are listed on this page.
Syntax
=SORTBY(array, by_array1, [sort_order1], [(by_array2, [sort_order2]), ...])
Arguments
| Argument | Type | Required | Default |
|---|---|---|---|
array | range (range) | Required | — |
by_array1 | range (range) | Required | — |
sort_order1 | number | Optional | 1 |
by_array2 | value | Optional | — |
sort_order2 | value | Optional | — |
Accepts a variable number of arguments. Minimum: 2.
Remarks
- Column criteria sort rows; row criteria sort columns. All criteria must use the same axis.
sort_ordermust be1or-1and defaults to ascending when omitted.- Additional
by_array/sort_ordercriteria are processed left-to-right. - Returns a spilled sorted array.
Error cases
- Shape mismatches or invalid criteria return
#VALUE!.
Examples
Sort names by a separate list of numbers.
Formula =SORTBY({"Cy";"Ana";"Bo"},{30;10;20})
Expected result {"Ana"; "Bo"; "Cy"}
Try this formula
This editor runs the published Formualizer 0.14.0 package, so the last digits can differ and functions newer than that release return #NAME?.
Sort ascending by the first numeric key, then break ties with a descending second key.
Formula =SORTBY({"Cy";"Ana";"Bo"},{20;10;20},1,{3;1;2},-1)
Expected result {"Ana"; "Cy"; "Bo"}
Try this formula
This editor runs the published Formualizer 0.14.0 package, so the last digits can differ and functions newer than that release return #NAME?.
Sort a one-item array with one corresponding key.
Formula =SORTBY({"Ana"},{10})
Expected result Ana
Try this formula
This editor runs the published Formualizer 0.14.0 package, so the last digits can differ and functions newer than that release return #NAME?.
The sort-key array must align with the sorted data.
Formula =SORTBY({"Cy";"Ana";"Bo"},{30;10})
Expected result #VALUE!
Try this formula
This editor runs the published Formualizer 0.14.0 package, so the last digits can differ and functions newer than that release return #NAME?.
Sort names by score
Formula =SORTBY(A1:A3,B1:B3)
| A | B | |
|---|---|---|
| 1 | "Charlie" | 3 |
| 2 | "Alice" | 1 |
| 3 | "Bob" | 2 |
Expected result {"Alice"; "Bob"; "Charlie"}
Try this formula
This editor runs the published Formualizer 0.14.0 package, so the last digits can differ and functions newer than that release return #NAME?.
Sort descending by key
Formula =SORTBY(A1:A3,B1:B3,-1)
| A | B | |
|---|---|---|
| 1 | "Q1" | 100 |
| 2 | "Q2" | 300 |
| 3 | "Q3" | 200 |
Expected result {"Q2"; "Q3"; "Q1"}
Try this formula
This editor runs the published Formualizer 0.14.0 package, so the last digits can differ and functions newer than that release return #NAME?.
Related functions
FAQ
How are multiple sort criteria applied?
SORTBY evaluates criteria left-to-right, using later by_array values only when earlier criteria compare equal.
Why do I get #VALUE! with SORTBY?
Each by_array must be one-dimensional and aligned to the same row or column axis; mismatched shapes return #VALUE!.