The SORTN function in Google Sheets automatically sorts your data and returns a specified number of the first results. A highly common use case for this formula is instantly extracting the top n or bottom n values from a massive dataset.
One common use case is to find the top n or the bottom n values in a dataset.
For example, here’s how you use SORTN to extract the top 5 values in a dataset:

with this formula:
=SORTN(A2:C21,5,0,3,FALSE)
SORTN Function Syntax
The core structure for this formula is =SORTN(range, [n], [display_ties_mode], [sort_column1, is_ascending1], [sort_column2, is_ascending2], ...). It requires a minimum of one argument to function, but typically utilizes three or five.
- range: The specific block of data that you want to sort and extract the first n rows from.
- n: The exact number of items you want to return. This must be greater than 0. It is an optional argument and automatically defaults to 1 if left blank.
- display_ties_mode: A numerical setting that dictates how the formula handles ties (equal values):
0– Ignores ties and strictly shows the first n rows.1– Shows the first n rows plus any additional rows that tie with the nth row.2– Shows the first n rows but executes data deduplication to remove any exact duplicates.3– Shows the first n unique rows alongside all duplicates of those specific rows.
- sort_column1: The index number of the column you want to sort by, or a specific range reference. If providing a range, it must contain the same number of rows as your primary range. If omitted, the formula defaults to the first column.
- is_ascending1: Determines the exact sorting algorithm direction.
TRUEsorts in ascending order (low to high), whileFALSEsorts in descending order (high to low). - [sort_column2, is_ascending2]: Optional pairs that allow you to add secondary sort columns and directions.
SORTN Function Examples
The following examples use this dataset (available in the template):

Example 1: No Optional Arguments
If you only include the range in your SORTN function, it returns a single value and sorts on the first column of your range.
=SORTN(A2:C21)
In this example, it sorts alphabetically from A-Z on column 1 and returns a single value.
The yellow highlighting indicates which cell the SORTN function is in.

Example 2: Top 5, No Sort Column Specified
Use the second argument to specify how many rows to return.
This example returns 5 rows:
=SORTN(A2:C21,5)

Example 3: Top 10, No Sort Column Specified
Here, this formula returns 10 rows:
=SORTN(A2:C21,10)

Example 4: Top 5 & Disregard Ties
The third argument determines how to treat ties, e.g. when there are two equal values in the n-th place.
This formula returns the first 5 values and disregards any ties:
=SORTN(A2:C21,5,0)
which looks like this in our Sheet:

Example 5: Top 5 & Disregard Ties & Specify Sort Column
The sort column must be paired with a TRUE/FALSE value that determines whether to sort ascending or descending. I.e. if you add the 4th argument then you must also add the 5th argument.
This formula selects the column at index 3 and sorts it descending, high to low:
=SORTN(A2:C21,5,0,3,FALSE)
which looks like this in our Sheet:

Example 6: Top 5 & Include Additional Tied Rows
Setting the third argument — how to deal with ties — to 1 tells the formula to include the additional ties of the nth row:
=SORTN(A2:C21,5,1,3,FALSE)
So in this case, we get the first 5 rows plus a sixth row because it ties with the 5th row:

Example 7: Top 5 & Remove Duplicates
However, if we set the third argument to 2, it removes any duplicate rows and shows the first n rows after removing duplicates.
=SORTN(A2:C21,5,2,3,FALSE)
Here you can see the SORTN formula has discarded the duplicate $900 and $740 values from example 6, and instead included a new 5th row with a value $600 that wasn’t in the previous data:

Example 8: Top 5 & Show First N Unique Rows & Duplicates
Finally, if you set the third argument to 3, it returns the first n unique rows and any duplicates of these rows.
So in this example, it returns 5 unique rows and 2 duplicate rows:
=SORTN(A2:C21,5,3,3,FALSE)
which looks like this in our Sheet:

Example 9: Bottom 5 (Sort Descending)
This example sorts the data from lowest to highest before returning 5 values. It picks the bottom 5 results:
=SORTN(A2:C21,5,0,3,TRUE)
It gives this output in our Sheet:

Example 10: Sort On Date Column To Get Most Recent 5
This example sorts on the date column (column index 2) instead of the values column:
=SORTN(A2:C21,5,0,2,FALSE)
This is the output in our Sheet:

Example 11: Top 5 & Two Sort Columns
Here’s an example sorting with two columns. Notice how they are both in pairs:
=SORTN(A2:C21,5,0,2,FALSE,3,FALSE)
It gives this output in our Sheet:

SORTN Function Template
When mastering new formulas for data analysis, practicing with view-only templates is highly recommended. You can safely duplicate template files into your own digital workspace by navigating to File > Make a copy. If your organization’s productivity software permissions restrict external document access, right-clicking the link and opening it inside an Incognito window usually bypasses the block. This versatile tool belongs to the Filter family of functions within Google Sheets.
One thought on “SORTN Function In Google Sheets”