The SORT function
SORT returns a sorted copy of a range and spills it into the cells below and to the right. The original data isn't touched, and the sorted copy updates as soon as the data changes.
Pick the column to sort by with sort_index and the direction with sort_order. It's ideal for leaderboards and live reports.
=SORT(array, [sort_index], [sort_order], [by_col])
Arguments
- array
- The range to sort. The original stays as it is.
- sort_indexoptional
- Which column of the range to sort by: 1 for the first, 2 for the second. The default is 1.
- sort_orderoptional
- 1 for ascending (A to Z, smallest first), the default; -1 for descending.
- by_coloptional
- TRUE to sort columns left to right instead of rows. Usually left out.
Try it
=SORT(A2:B6,2,-1) in D2. The table spills into D2:E6, sorted by column 2 (Sales) from highest to lowest. Gareth and Owen tie, so they keep their original order.
D2
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Rep | Sales | Rep | Sales | ||
| 2 | Aisha | £18,400 | Gareth | £22,150 | ||
| 3 | Gareth | £22,150 | Owen | £22,150 | ||
| 4 | Mei | £9,800 | Aisha | £18,400 | ||
| 5 | Owen | £22,150 | Freya | £15,300 | ||
| 6 | Freya | £15,300 | Mei | £9,800 | ||
| 7 | ||||||
| 8 |
Edit the formula in D2, or change the data, and press Enter.
More examples
| Formula | Result | What it does |
|---|---|---|
| =INDEX(SORT(A2:B6,2,-1),1,1) | Gareth | The top seller: row 1, column 1 of the sorted table. |
| =TEXTJOIN(", ",TRUE,SORT(A2:A6)) | Aisha, Freya, Gareth, Mei, Owen | With no other arguments, SORT goes A to Z by the first column. |
| =INDEX(SORT(A2:B6,2,1),1,1) | Mei | Ascending order puts the lowest sales first. |
Tips
- Combine with FILTER to sort only some rows:
=SORT(FILTER(A2:B6,B2:B6>15000),2,-1). - Sort by two columns with arrays of indexes and orders:
=SORT(A2:C20,{2,3},{1,-1}). - Format the spill area (here column E as £) before the results arrive, so they look right.
Common mistakes
- You get #SPILL!.Cells in the spill area aren't empty. Clear everything below and to the right of the formula.
- The headings get sorted into the data.Start the range on the first data row (A2, not A1) and type the headings above the formula.
- Numbers sort in the wrong order: 100 before 20.Those numbers are stored as text. Convert them to real numbers, for example with VALUE.
In Google Sheets
Google Sheets' SORT takes pairs of a column number and TRUE/FALSE instead: =SORT(A2:B6, 2, FALSE) sorts by column 2, descending (TRUE is ascending). Add more pairs to sort by more columns.
Practise SORT with instant feedback
Type real formulas into a working spreadsheet and get told straight away whether they work.