1. Home
  2. Functions
  3. Array
  4. SORTN

SORTN function

Returns the first n rows of a range after sorting, with options for ties.

Syntax

=SORTN(range, [n], [display_ties_mode], [sort_column], [is_ascending], ...)

Arguments

ArgumentRequiredDescription
rangeRequiredThe data to sort
nOptionalHow many rows to return (default 1)
display_ties_modeOptional0 first n rows (default), 1 also rows tied with the last one, 2 drop duplicate rows, 3 first n unique rows with all their duplicates
sort_columnOptionalThe column (from 1) to sort by
is_ascendingOptionalTRUE (default) ascending, FALSE descending

How to use SORTN

When SORTN returns more than one value, the result spills into the cells below and to the right. Keep those cells empty, or you get #SPILL!.

SORTN takes 1 required argument and 4 optional arguments, and accepts more values in the same pattern. Type =SORTN( in a cell and SheetsBat shows the arguments as you go. Optional arguments are shown in square brackets; leave them out to use their default.

Examples

Each result below was calculated by the SheetsBat formula engine.

=SORTN({3;1;2}, 2)Result 2 by 1 array
Spilled result
A
11
22
=SORTN({"a",3;"b",1;"c",2}, 1, 0, 2, FALSE)Result 1 by 2 array
Spilled result
AB
1a3

Every formula, right in your browser

SheetsBat calculates 536 functions, with charts, pivot tables and live collaboration. Free to start.