Syntax
=SORT(array, [sort_index], [sort_order], [by_col])Arguments
| Argument | Required | Description |
|---|---|---|
array | Required | The range or array to sort |
sort_index | Optional | Which column (or row) to sort by (default 1) |
sort_order | Optional | 1 ascending (default), -1 descending |
by_col | Optional | FALSE (default) sorts rows, TRUE sorts columns |
How to use SORT
When SORT returns more than one value, the result spills into the cells below and to the right. Keep those cells empty, or you get #SPILL!.
SORT takes 1 required argument and 3 optional arguments. Type =SORT( in a cell and SheetsBat shows the arguments as you go. Optional arguments are shown in square brackets; leave them out to use their default.
Examples
Each result below was calculated by the SheetsBat formula engine.
=SORT({3;1;2})Result Spilled result
| A | |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
=SORT({"b",2;"a",1;"c",3}, 2, -1)Result Spilled result
| A | B | |
|---|---|---|
| 1 | c | 3 |
| 2 | b | 2 |
| 3 | a | 1 |
=SORT({1;2}, 3)Result Related array functions
- QUERYRuns a query (select, where, group by, order by, limit, offset, label) over a range with optional header rows.
- REDUCEReduces an array to one value by applying a LAMBDA to an accumulator and each value.
- SCANApplies a LAMBDA to each value with a running accumulator and returns every intermediate result.
- SEQUENCEReturns a list of sequential numbers in an array of the given size.
- SORTBYSorts a range or array by the values of other ranges or arrays.
- SORTNReturns the first n rows of a range after sorting, with options for ties.
- TAKEReturns a number of rows or columns from the start (positive) or end (negative) of an array.
- TOCOLReturns the values of an array as a single column.
- TOROWReturns the values of an array as a single row.