How to Use the SORTN Function in Google Sheets | Extract and Sort Top N Items
What Is the SORTN Function?
The SORTN function sorts a specified data range and returns only the top (or bottom) n items. It lets you extract top-3 sales rankings or the latest entries in a single formula.
Syntax
=SORTN(range, [n], [display_ties_mode], [sort_column1, is_ascending1], ...)
- range: The cell range to sort and extract from.
- n (optional): The number of items to return (defaults to
1). - display_ties_mode (optional): A number from
0to3specifying how ties are handled (defaults to0):0: Show at most the firstnrows in the sorted range1: Show at most the firstnrows, plus any additional rows identical to then-th row2: Show at most the firstnrows after removing duplicate rows3: Show at most the firstnunique rows, plus all duplicates of those rows
- sort_column1 (optional): The column index (with the leftmost column of
rangeas1) or a single column outsiderangeto sort by. - is_ascending1 (optional):
TRUEfor ascending order (smallest to largest),FALSEfor descending order (largest to smallest).
Practical Examples
This example extracts the top 3 scorers (column 2) from test results in A2:B6, including ties.
| Row | Col A (Name) | Col B (Score) | Formula in Col D | Col D (Extracted Name) | Col E (Extracted Score) |
|---|---|---|---|---|---|
| 1 | Name | Score | =SORTN(A2:B6, 3, 1, 2, FALSE) |
Suzuki | 95 |
| 2 | Sato | 80 | (Auto-expanded) | Tanaka | 90 |
| 3 | Suzuki | 95 | (Auto-expanded) | Takahashi | 85 |
| 4 | Takahashi | 85 | (Auto-expanded) | Watanabe | 85 |
| 5 | Tanaka | 90 | |||
| 6 | Watanabe | 85 |
Note: Because display_ties_mode is set to 1, both Takahashi and Watanabe—who are tied for 3rd place (85 points)—are extracted, outputting 4 rows in total.
Common Errors and Precautions
#VALUE!error: Occurs ifsort_columnexceeds the number of columns inrange, or if an external sort column does not match the height (row count) ofrange.#REF!error: Occurs if any cell in the output expansion area already contains a value. Clear the destination area before entering the formula.- Do not include header rows: Including a header row (such as row 1) in
rangewill cause the text headers to be sorted along with the data. Specify only the data rows.
Frequently Asked Questions
Q. How do I extract the 'bottom N' items instead of the 'top N' with SORTN?
A. Set the fourth argument (is_ascending) to FALSE? Wait—set it to TRUE (ascending order). Because values are sorted from smallest to largest, the bottom N items are extracted.
Q. How are ties (duplicate values) at the cutoff of N items handled?
A. You can control tie behavior with the third argument 'display_ties_mode'. Specifying 0 outputs strictly N rows, while specifying 1 outputs all tied rows as well.