Formualizer Docs

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

ArgumentTypeRequiredDefault
arrayrange (range)Required—
by_array1range (range)Required—
sort_order1numberOptional1
by_array2valueOptional—
sort_order2valueOptional—

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_order must be 1 or -1 and defaults to ascending when omitted.
  • Additional by_array/sort_order criteria 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)

Input cells
AB
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)

Input cells
AB
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?.

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!.

Implementation

Engine source

On this page