ShareYourGroupShareYourGroupGoogle Sheets

Only in Sheets

SORT and SORTN functions in Google Sheets

SORT returns a range in sorted order; SORTN returns only the first few rows after sorting.

SORT gives you a sorted copy of your data that re-sorts itself when the data changes. The original stays in the order it was entered. SORTN goes one step further and returns only the top few rows, which makes "top five" lists a one-formula job.

Syntax

=SORT(range, sort_column, is_ascending, [sort_column2, is_ascending2], ...)
=SORTN(range, [n], [display_ties_mode], [sort_column, is_ascending], ...)
ArgumentWhat it is
rangeThe data to sort.
sort_columnThe number of the column to sort by, counted from the left of the range, or a separate range of the same height.
is_ascendingTRUE for A to Z and smallest first. FALSE for the reverse.
nFor SORTN: how many rows to return.
display_ties_modeFor SORTN: 0 returns exactly n rows. 1 also returns rows that tie with the last one. 2 removes duplicate rows first. 3 returns n distinct rows with their duplicates.

Examples

Sample sheet: A small price list with item codes. The table is sorted by code.
ABCDE
1CodeItemCategoryPriceStock
2A101Basmati Rice 5 kgGrocery65040
3A102Toor Dal 1 kgGrocery16075
4B201NotebookStationery60300
5B202Ball PenStationery100
6C301LED BulbElectronics110200
7C302Power BankElectronics129918
8D401Steel BottleHome45055

How SORT works

After the range, arguments come in pairs: which column to sort by, and TRUE for ascending or FALSE for descending. Add more pairs for further levels. The second example sorts by category and, inside each category, by name.

The sort column can be given in two ways.

With the range alone and nothing else, SORT uses the first column, ascending.

SORTN: the first n rows

SORTN sorts and then keeps only the first n rows. =SORTN(B2:D8, 3, 0, 3, FALSE) reads: from this range, return 3 rows, with tie mode 0, sorted by column 3 descending.

The third argument decides what happens with ties and duplicates.

ModeReturns
0exactly n rows
1n rows, plus any further rows that tie with the nth
2n rows after removing duplicate rows
3n distinct rows, along with all their duplicates

Mode 1 is the fair one for a prize list: if two people share third place, both appear.

A sorted, filtered list

SORT wraps naturally around FILTER: =SORT(FILTER(A2:F13, C2:C13="North"), 6, FALSE) shows the North sales, largest first. Remember that the column number then counts inside the filtered result.

A custom order

To sort by your own order, such as Small, Medium, Large, give SORT the position of each value in that list: =SORT(A2:C20, MATCH(B2:B20, {"Small";"Medium";"Large"}, 0), TRUE).

Sorting in random order

=SORT(A2:A20, RANDARRAY(19), TRUE) shuffles a list of nineteen names. It reshuffles whenever the sheet recalculates, so copy the result and paste it as values to keep one order.

Things to know

SORT, the menu, or QUERY?

Common mistakes

In Excel

Excel's SORT is =SORT(array, sort_index, sort_order), where the order is 1 or -1 instead of TRUE or FALSE, and several levels are given as lists in curly brackets. Sorting by an outside range is a separate function there, SORTBY. Excel has no SORTN.

Related functions

The results on this page were worked out by us from the sample sheet; they were not produced by a script running Google Sheets. If Sheets shows you something different, please tell us.

This is an independent help site. It is not connected with or approved by Google. Google Sheets is a trademark of Google LLC, named here only to say which program a formula is for. The people and figures in the sample sheets are made up.