SORT
Sorts an array by a selected row or column and returns a spilled result.
SORT can order rows (default) or columns.
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
=SORT(array, [sort_index], [sort_order], [by_col])
Arguments
| Argument | Type | Required | Default |
|---|---|---|---|
array | range (range) | Required | — |
sort_index | number | Optional | 1 |
sort_order | number | Optional | 1 |
by_col | logical | Optional | false |
Remarks
- Defaults:
sort_index=1,sort_order=1(ascending),by_col=FALSE. sort_indexis 1-based in the active sort axis.sort_order < 0sorts descending; otherwise ascending.- Empty input returns an empty spill.
Error cases
- Invalid sort indexes return
#VALUE!.
Examples
Sort a column in ascending order.
Formula =SORT({30;10;20})
Expected result {10; 20; 30}
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 two-column table descending by its first column.
Formula =SORT({30,"Cy";10,"Ana";20,"Bo"},1,-1)
Expected result {30, "Cy"; 20, "Bo"; 10, "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?.
Sorting a one-element array leaves the element in place.
Formula =SORT({42})
Expected result 42
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?.
A sort index outside the available columns cannot select a key.
Formula =SORT({30;10;20},2)
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 rows by second column
Formula =SORT(A1:B3,2,1,FALSE)
| A | B | |
|---|---|---|
| 1 | "C" | 30 |
| 2 | "A" | 10 |
| 3 | "B" | 20 |
Expected result {"A", 10; "B", 20; "C", 30}
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 columns by first row descending
Formula =SORT(A1:C2,1,-1,TRUE)
| A | B | C | |
|---|---|---|---|
| 1 | 1 | 3 | 2 |
| 2 | "A" | "C" | "B" |
Expected result {3, 2, 1; "C", "B", "A"}
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
What changes when by_col is TRUE?
SORT reorders columns instead of rows, and sort_index is interpreted as a row index used as the sort key.
What causes #VALUE! in SORT?
If sort_index is outside the active axis (row or column axis based on by_col), SORT returns #VALUE!.