Google Sheets function · Filter and sort
SORT in Google Sheets: syntax and examples
SORT returns the rows of a range in a new order, by one or several columns, without changing the original data. The result spills into the cells below and to the right of the formula.
Syntax
SORT(range, sort_column, is_ascending, [sort_column2], [is_ascending2])
Arguments
| Argument | Type | Required | What it does |
|---|---|---|---|
range | range | yes | The rows to sort. The header row should stay outside. |
sort_column | number | yes | Which column of range to sort by, counted from 1 inside range. |
is_ascending | boolean | yes | TRUE for A to Z and small to large, FALSE for the reverse. |
sort_column2 | number | no | A second column used to break ties. |
is_ascending2 | boolean | no | Direction for the second column. |
Examples
The examples use this table, starting in A1:
| Region | Product | Units | Price |
| North | Pen | 10 | 2.5 |
| South | Pen | 4 | 2.5 |
| North | Book | 3 | 12 |
| East | Book | 8 | 12 |
| South | Lamp | 5 | 30 |
=SORT(A2:D6, 3, FALSE)Result:
| North | Pen | 10 | 2.5 |
| East | Book | 8 | 12 |
| South | Lamp | 5 | 30 |
| South | Pen | 4 | 2.5 |
| North | Book | 3 | 12 |
All rows ordered by units, largest first. The result spills into a block that starts in the formula cell.
Run in a Google Sheet (English US locale) on .
=SORT(A2:D6, 2, TRUE, 3, FALSE)Result:
| East | Book | 8 | 12 |
| North | Book | 3 | 12 |
| South | Lamp | 5 | 30 |
| North | Pen | 10 | 2.5 |
| South | Pen | 4 | 2.5 |
Two keys: product A to Z first, then units from large to small inside each product.
Run in a Google Sheet (English US locale) on .
=SORT(A2:A6, 1, FALSE)Result:
| South |
| South |
| North |
| North |
| East |
One column only, Z to A. Duplicates stay.
Run in a Google Sheet (English US locale) on .
=SORT(FILTER(A2:C6, C2:C6>4), 3, TRUE)Result:
| South | Lamp | 5 |
| East | Book | 8 |
| North | Pen | 10 |
SORT on top of FILTER: keep rows with more than 4 units, then order them upward.
Run in a Google Sheet (English US locale) on .
=SORT(A1:D6, 3, TRUE)Result:
| North | Book | 3 | 12 |
| South | Pen | 4 | 2.5 |
| South | Lamp | 5 | 30 |
| East | Book | 8 | 12 |
| North | Pen | 10 | 2.5 |
| Region | Product | Units | Price |
The header row is inside the range here, so the text Units is sorted like data and lands at the bottom.
Run in a Google Sheet (English US locale) on .
Common errors
- is_ascending is required, even though it looks like a small detail. =SORT(A2:D6, 3) returns #N/A. Write TRUE or FALSE.
- sort_column counts inside range, not on the sheet. A number higher than the width of range raises no error: the rows come back in their original order, which looks like SORT did nothing.
- If the header row is inside range, it is sorted with the data. In a number column sorted upward it ends at the bottom. Leave it outside.
Compared with Excel
Excel 365 has SORT(array, [sort_index], [sort_order], [by_col]), where the order is 1 or -1 and the column is optional. Sheets asks for TRUE or FALSE and takes extra keys as column and direction pairs.