Going Stellar

Home/Google Sheets reference

Google Sheets function · Filter and sort

SORTN in Google Sheets: syntax and examples

SORTN sorts a range and returns only the first n rows, so one formula gives a top 3 or a bottom 5. The third argument decides what happens to ties at the cut. It can also remove duplicate rows.

Syntax

SORTN(range, [n], [display_ties_mode], [sort_column1, is_ascending1], ...)

Arguments

ArgumentTypeRequiredWhat it does
rangerangeyesThe data to sort and cut. Several columns are fine.
nnumbernoHow many rows to return. Without it, 1.
display_ties_modenumbernoWhat to do with ties at the cut: 0 keeps exactly n rows, 1 adds the rows tied with the last one, 2 removes duplicate rows.
sort_column1, is_ascending1number, booleannoThe column number inside range to sort by, and TRUE for ascending or FALSE for descending. Can repeat.

Examples

The examples use this table, starting in A1:

NameScoreTeam
Ana90Red
Bob85Blue
Cris85Red
Dan70Green
Eva60Blue
=SORTN(A2:B6, 3, 0, 2, FALSE)

Result:

Ana90
Bob85
Cris85

Top 3 by score, highest first. The 2 is the column of range to sort by.

Run in a Google Sheet (English US locale) on .

=SORTN(A2:B6, 2, 1, 2, FALSE)

Result:

Ana90
Bob85
Cris85

Top 2, but mode 1 also keeps Cris, who ties with Bob at 85.

Run in a Google Sheet (English US locale) on .

=SORTN(A2:B6, 2, 0, 2, FALSE)

Result:

Ana90
Bob85

The same top 2 with mode 0: the tie is cut and only Ana and Bob remain.

Run in a Google Sheet (English US locale) on .

=SORTN(A2:B6, 1, 0, 2, TRUE)

Result:

Eva60

The lowest score: ascending order, one row.

Run in a Google Sheet (English US locale) on .

=SORTN(C2:C6, 3, 2, 1, TRUE)

Result:

Blue
Green
Red

Mode 2 removes duplicates first: the distinct teams in alphabetical order.

Run in a Google Sheet (English US locale) on .

Common errors

  • sort_column1 is a position inside range (1 is its first column), not a sheet column letter. In A2:B6, 2 sorts by Score and 1 sorts by Name.
  • With mode 0, a tie at the cut is broken by the original order: the top 2 below keeps Bob and drops Cris, who also scored 85. Use mode 1 when ties must stay.
  • Mode 1 can return more than n rows. SORTN(A2:B6, 2, 1, 2, FALSE) returns 3, because Cris ties with Bob.
  • An unknown display_ties_mode such as 9 returns #VALUE!. Use 0, 1 or 2.

Compared with Excel

SORTN exists in Sheets only. Excel has no direct match: use TAKE(SORT(...), n) in Microsoft 365.

Sources

  1. SORTN (Docs Editors Help)

Markdown version · All functions