## Purpose

## Return value

## Arguments

*array*- Range or array to sort.*sort_index*- [optional] Column index to use for sorting. Default is 1.*sort_order*- [optional] 1 = Ascending, -1 = Descending. Default is ascending order.*by_col*- [optional] TRUE = sort by column. FALSE = sort by row. Default is FALSE.

## Syntax

## How to use

The SORT function sorts the contents of a range or array in ascending or descending order with a formula. The result from SORT is a dynamic array of values that will "spill" onto the worksheet into a range. If values in the source data change, the result from SORT will update automatically.

The SORT function takes four arguments: *array*, *sort_index*, *sort_order*, and *by_col*. The first argument, *array*, is the range or array to be sorted. This is the only required argument. By default, the SORT function will sort values in ascending order using the *first* column in *array*. Use the optional arguments *sort_index* and *sort_order* to control which column to sort by, and the order to sort by (ascending or descending). *Sort_index* should be a number corresponding to the column (or row) used for sorting. For example, to sort by the third column in a range of data, use 3 for *sort_index*. The optional *sort_order* argument determines sort direction. Use 1 for *ascending* order and -1 for *descending* order. By default, the SORT function will sort data vertically by rows. To sort a range horizontally by columns, set the fourth argument, *by_col*, to TRUE.

### Basic Examples

To sort a range by the first column in *ascending* order:

```
=SORT(range) // sort by column 1, ascending
=SORT(range,1,1) // sort by column 1, ascending
```

To sort a range by the *first* column in *descending* order:

```
=SORT(range,1,-1) // sort by column 1, descending
```

To sort a range by the *second* column in *descending* order:

```
=SORT(range,2,-1) // sort by column 2, descending
```

To sort a range horizontally (by column) using values in row 1 in descending order, set the fourth argument, *by_col*, to TRUE or 1:

```
=SORT(range,1,-1,TRUE)
=SORT(range,1,-1,1)
```

### Example - sort by score

In the example shown above, data includes names in column B and scores in column C. In cell E5, the SORT function is used to sort the data by score in descending order:

```
=SORT(B5:C14,2,-1) // sort by scores in descending order
```

The SORT function extracts all values, sorted in descending order by score, and results "spill" into the range E5:F14. To sort by score in *ascending* order, omit *sort_order* or to 1 like this:

```
=SORT(B5:C14,2) // sort by score in ascending order
=SORT(B5:C14,2,1) // sort by score in ascending order
```

### Sort by more than one level

Unlike the SORTBY function, the SORT function does not provide a way to sort data by more than one level. However, by using array constants for *sort_index* and *sort_order*, it can be done. This video explains how.

### Notes

- SORT returns a #VALUE error if
*sort_index*is out of range.