The SORTN function lets you sort a range of data and return the first n items from that range. The function creates a new range of data with the new, sorted output from the function. SORTN can significantly reduce the number of steps that it takes to get the data that you need out of a table.

SORTN is similar to FILTER, which filters a specified range, but SORTN returns the highest n amounts while FILTER returns the matching amounts.

Excel, the most popular spreadsheet program, does not have this function.

Purpose

Returns n number of rows from a range of data.

Video explanation

Syntax

=SORTN(range,[n],[display_ties_mode],[sort_column1, is_acending1], ...)

  • range - The entire range of data that is to be sorted
  • n - The number of items to return
  • display_ties_mode [OPTIONAL] - An optional number designating the way to display ties.
    • 0: Show at most the first n rows in the sorted range.
    • 1: Show at most the first n rows, plus any additional rows that are identical to the nth row.
    • 2: Show at most the first n rows after removing duplicate rows.
    • 3: Show at most the first n unique rows, but show every duplicate of these rows.
    sort_column1 [OPTIONAL] - The column of data that contains the criteria that you are sorting. Can only be one column at a time. If this value is not specified, the lowest index column in the range will be used and subsequent columns will be used if a tie-breaker is needed.
  • is_ascending1 [OPTIONAL] - Two possible answers - TRUE for ascending (1,2,3), FALSE for descending (3,2,1)
  • sort_column2 - [OPTIONAL] - Add as many of these as you want. On and on....

Tip: When using SORTN, start your formula in a cell that should be the upper left most cell for the output. This function writes the output in, below, and to the right of your starting point. The formula text will remain in the upper left cell and can be changed.

Tip: If sort_column1 and is_ascending1 aren't included, the sort is performed on the lowest-index column in range, with subsequent columns used to sort if there are ties.

Examples

Example 1

Using a simple SORTN function with one column of data and extracting the two highest values. The display_ties is set to 0 which means show two highest values regardless of ties. The zero isn't specified, but it is the default. This is the same as typing "0".

Shows the starting data and the formula typed into the last cell.
A
12
25
33
43
5=SORTN(A1:A4,2)
6
The first three rows are the original data and the last two rows are the dynamic results of the function. The function is still in cell A5 and you can see it if you select that cell.
A
12
25
33
43
52
63

Example 2

The display_ties parameter has been changed to "1" in this example which tells Sheets to show the two highest values including any ties. The two first values are both 2, and it has included all three occurrences of 2.

Shows the starting data and the formula typed into the last cell.
A
12
22
32
45
53
63
7=SORTN(A1:A6,2,1)
8
9
The first six rows are the original data and the last three rows are the dynamic results of the function. The function is still in cell A5 and you can see it if you select that cell.
A
12
22
32
45
53
63
72
82
92

Example 3

The display_ties parameter is set to 2 which means show at most only 2 rows but don't show any duplicates

Shows the starting data and the formula typed into the last cell.
A
12
22
32
45
53
63
7=SORTN(A1:A6,2,2)
8
The first six rows are the original data and the last two rows are the dynamic results of the function. The function is still in cell A7 and you can see it if you select that cell.
A
12
22
32
45
53
63
72
83

Example 4

The display_ties parameter is set to 3 which means show at most only 2 items but show all duplicates of these items. 2 and 3 are the two highest items so it is showing all 2s and 3s no matter how many there are. Note: It seems to me that this function should also be showing all of the threes, but it is not. I submitted this as a question on the Google Docs forum and will update this notes with what I find out.

Alert! This function, as of early September 2017, is not working properly. If you look at the shared Sheet, starting at row 33, the function is not returning all of the 3's that it should. The bug has been reported to Google here.
Shows the starting data and the formula typed into the last cell.
A
12
22
32
45
53
63
7=SORTN(A1:A6,2,3)
8
9
10
11
The first six rows are the original data and the last four rows are the dynamic results of the function. The function is still in cell A7 and you can see it if you select that cell.
A
12
22
32
45
53
63
72
82
92
103
113

Example 5

Two column SORTN, only sorted one column.

Shows the starting data and the formula typed into row 5.
AB
1HenBrown
2SheepWhite
3Bullbrown
4GoatWhite
5=SORTN(A1:A4,2,1)
6
Because I specified only the first column of my range, the output of the formula was only the first column.
AB
1Henbrown
2SheepWhite
3Bullbrown
4Goatwhite
5Bull
6Goat

Example 6

Two column SORTN, specifying both columns in the range.

Shows the starting data and the formula typed into row 5. Notice that the range now is both column A and column B.
AB
1HenBrown
2SheepWhite
3Bullbrown
4GoatWhite
5=SORTN(A1:B4,2,1)
6
Because I specified both columns of this range, the output of the formula is both columns.
AB
1Henbrown
2SheepWhite
3Bullbrown
4Goatwhite
5Bullbrown
6Goatwhite

 Live examples in Sheets 

Go to this spreadsheeet for several examples of the SORTN function that you can study and use anywhere you would like.