SORTN Function
The SORTN function in Google Sheets sorts a range and returns a specified number of sorted rows.
Syntax
SORTN(range, [n], [display_ties_mode], [sort_column1, is_ascending1], ...)
range
: The range of cells to sort. This is a required parameter.n
: (Optional) The number of rows to return. Defaults to the entire range.display_ties_mode
: (Optional) Mode for handling ties.sort_column1
: (Optional) The index or reference to the first column to sort by.is_ascending1
: (Optional) TRUE to sort in ascending order, FALSE for descending order.
Examples
- Return Top 3 Rows
Sort the range A1:B10 by column A and return the top 3 rows:
=SORTN(A1:B10, 3, 0, 1, TRUE)
This will return the top 3 sorted rows.
- Handle Ties
Sort the range A1:B10 by column A, return the top 3 rows, and include ties:
=SORTN(A1:B10, 3, 1, 1, TRUE)
This will return the top 3 rows and any tied values.
Notes
- The function allows flexible sorting criteria and returns only the specified number of rows.
- It supports tie-breaking based on additional columns.