Google Sheets Tips

SORT and SORTN in Google Sheets: syntax, top N and ties

SORT returns a sorted copy of a range, by one or several columns, and leaves the source untouched. SORTN returns only the first N rows of the sorted result and has four modes for ties and duplicates. This guide shows the syntax of both, sorting by several columns and by a custom order, the tie modes on one example, and the mistakes that return the wrong rows.

Google Sheets TipsVadym Kramarenko9 min read

SORT and SORTN in Google Sheets: syntax, top N and ties

SORT returns a sorted copy of a range. SORTN returns only the first rows of that sorted copy.

=SORT(B3:D10, 3, FALSE)

=SORTN(B3:D10, 3, 0, 3, FALSE)

The first formula returns all rows of B3:D10, ordered by the third column from the largest value down. The second returns the top three of them. Neither changes the source: the result appears next to it and updates when the data changes.

SORT

SORT

=SORT(range, sort_column, is_ascending, [sort_column2, is_ascending2, ...])

  • range: the data to sort, without its header row.
  • sort_column: the number of the column inside the range to sort by, or a separate range of the same height.
  • is_ascending: TRUE for A to Z and smallest first, FALSE for the reverse.
  • More pairs of column and direction break ties in the order they are listed.

With the range alone, =SORT(B3:B10), the result is sorted by the first column in ascending order.

=SORT(B3:D10, 3, TRUE)

Google Sheets with names, employee IDs and joining dates and the formula =SORT(B3:D10, 3, TRUE) returning the rows from the earliest joining date to the latest

Ascending and descending

=SORT(B3:C10, 2, TRUE)

=SORT(B3:C10, 2, FALSE)

Google Sheets with names and performance scores and two results: sorted by score in ascending order with TRUE and in descending order with FALSE

Several columns

=SORT(B3:C10, 2, TRUE, 1, TRUE)

Google Sheets with names and departments and the formula =SORT(B3:C10, 2, TRUE, 1, TRUE) returning the rows by department, and by name inside each department

The first pair is the main order, the second decides between rows that are equal in the first.

Sort by a range, also one outside the data

The sort column can be a range instead of a number. That makes the formula independent of the column’s position, and allows sorting by a column that is not part of the output:

=SORT(C3:C10, B3:B10, FALSE)

Google Sheets with names in column B and employee IDs in column C and the formula =SORT(C3:C10, B3:B10, FALSE) returning the IDs in the reverse alphabetical order of the names

The same form sorts by a calculated value. For a custom order that is not alphabetical, sort by the position of each value in a list:

=SORT(B3:D10, MATCH(C3:C10, {"High"; "Medium"; "Low"}, 0), TRUE)

MATCH returns 1 for “High”, 2 for “Medium” and 3 for “Low”, and SORT orders the rows by those numbers. And for most frequent values first:

=SORT(B3:C10, COUNTIF(C$3:C$10, C3:C10), FALSE, 2, TRUE)

Google Sheets with names and salaries and a SORT formula over COUNTIF returning the rows with the most common salary first

Data from another tab, open-ended ranges

=SORT(Dataset!B3:E, 3, TRUE)

Google Sheets with the formula =SORT(Dataset!B3:E, 3, TRUE) returning the rows of the Dataset tab sorted by department

A range without an end row, B3:E, includes rows added later. It also includes the empty rows below the data; wrap the range in FILTER to keep them out of the result: =SORT(FILTER(B3:E, B3:B <> ""), 3, FALSE).

How values are ordered

  • Numbers come before text, and text before TRUE and FALSE.
  • Text is ordered without regard to case.
  • Numbers stored as text are sorted as text: “10”, “100”, “9”. Convert them with VALUE first.

SORTN

SORTN

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

  • n: how many rows to return. 1 if omitted.
  • display_ties_mode: what to do with ties and duplicates, a number from 0 to 3.
  • sort_column, is_ascending: as in SORT. If omitted, the range is sorted by its first column in ascending order.

=SORTN(B3:D10, 3, 0, 3, FALSE)

Google Sheets with the formula =SORTN(B3:D10, 3, 0, 3, FALSE) returning the three rows with the latest joining dates

Top and bottom N

=SORTN(B3:C10, 5, 0, 2, FALSE)

Google Sheets with names and performance scores and the formula =SORTN(B3:C10, 5, 0, 2, FALSE) returning the five highest scores

For the bottom five, sort in ascending order:

=SORTN(B3:C10, 5, 0, 2, TRUE)

Google Sheets with the formula =SORTN(B3:C10, 5, 0, 2, TRUE) returning the five lowest values

The four tie modes

The modes differ only when values repeat. In this table two people earn 55,000 and two earn 50,000, and the top of the salary column reads 70,000, 60,000, 58,000, 55,000, 55,000, 52,000.

Mode Returns SORTN(B3:D10, n, mode, 3, FALSE)
0 Exactly n rows n = 5: 70,000, 60,000, 58,000, 55,000, 55,000
1 n rows, plus any rows tied with the last one n = 4: the same five rows, because the fifth ties with the fourth
2 n rows after duplicates are removed n = 5: 70,000, 60,000, 58,000, 55,000, 52,000
3 The first n distinct values, with all their duplicates n = 5: six rows, both 55,000 rows included

Google Sheets with SORTN in mode 0 returning exactly five rows by salary

Google Sheets with the formula =SORTN(B3:D10, 4, 1, 3, FALSE) returning five rows, the fourth and fifth both with a salary of 55000

Google Sheets with the formula =SORTN(B3:D10, 5, 2, 3, FALSE) returning five rows with five different salaries

Google Sheets with the formula =SORTN(B3:D10, 5, 3, 3, FALSE) returning six rows: five different salaries, with both rows of 55000

Mode 1 is the fair one for leaderboards: nobody is cut off by a tie. Mode 2 with a large n is a way to remove duplicates by one column: =SORTN(B3:D10, 9^9, 2, 2, TRUE) returns one row for each value of the second column. More ways in the guide to removing duplicates.

The sort column is not optional in practice

Without a sort column, SORTN sorts by the first column of the range in ascending order, whatever order the data had. =SORTN(B3:D10, 3) does not return “the first three rows”: it returns the first three after sorting by column B. A range that is already sorted, for example by a QUERY with ORDER BY, is re-sorted and the top three are lost. Always name the column and the direction.

The Turning Point

OWOX Data Marts

See your first report built in real time. 15 minutes.

  1. Connect your data warehouse
  2. Pick your metrics
  3. Get a live Google Sheets report

In the time it takes to write a ticket. Then imagine never writing that ticket again.

Book a Demo

We'll use your actual use case

SORT and SORTN with other functions

SORT and SORTN with other functions

FILTER: sort only the rows you need

=SORT(FILTER(B3:D10, C3:C10="Sales", D3:D10>45000), 3, TRUE, 1, TRUE)

Google Sheets with a SORT formula over FILTER returning the Sales rows with a salary above 45000, sorted by salary and name

For “either condition”, add the conditions inside FILTER: (C3:C10="Sales") + (D3:D10>55000).

UNIQUE: a sorted list without repeats

=SORT(UNIQUE(D3:D10), 1, TRUE)

Google Sheets with salaries in column D and the formula =SORT(UNIQUE(D3:D10), 1, TRUE) returning each salary once, from the lowest

See UNIQUE.

TRANSPOSE: sort data laid out in a row

SORT orders rows. For data that runs across, turn it, sort it and turn it back:

=TRANSPOSE(SORT(TRANSPOSE(C2:J3), 1, TRUE))

Google Sheets with employee IDs and names laid out in two rows and a formula with TRANSPOSE and SORT returning the columns in ascending order of ID

QUERY: filter the sorted result

=QUERY(SORT(B3:C10, 2, FALSE), "SELECT * WHERE Col2 >= 55000")

Google Sheets with a QUERY formula over SORT returning the rows with a salary of at least 55000, from the highest

QUERY can do both steps itself with WHERE and ORDER BY; use whichever reads better.

SORT, SORTN or the menu

SORT, SORTN or the menu

  • Data > Sort range rearranges the cells themselves, once. Use it for a table you maintain by hand. See how to sort data.
  • SORT leaves the source alone and stays up to date. Use it for a report built on data that changes.
  • SORTN is SORT with a row limit and tie handling. Use it for top lists.

Errors and wrong results

Errors and wrong results

⚠️ Error: #REF! with “Array result was not expanded”.

✅ Solution: The result needs cells that are not empty. Clear the space below and to the right of the formula.

⚠️ Error: An error after a sort column number was typed.

✅ Solution: The number is larger than the number of columns in the range. Columns are counted from the left edge of the range, not of the sheet: in B3:D10, column D is 3.

⚠️ Error: The header row ends up in the middle of the result.

✅ Solution: The range includes the header. Start the range one row lower.

⚠️ Error: Numbers are in the order 10, 100, 9.

✅ Solution: They are stored as text. Convert the column to numbers.

⚠️ Error: Empty rows in the result.

✅ Solution: The range is open-ended and includes the blank rows below the data. Filter them out before sorting.

⚠️ Error: SORTN returns the wrong rows.

✅ Solution: The sort column or direction is missing, so the range was sorted by its first column in ascending order. Add them.

⚠️ Error: SORTN returns more rows than n.

✅ Solution: Tie mode 1 or 3 adds the tied rows. Use mode 0 for an exact count.

Related guides

A top list is only as complete as its rows

A top list is only as complete as its rows

SORTN gives an exact top five of the rows in the range. If the range is an export that stops at last Tuesday, or lacks one region, the list is exact and wrong, and a ranking is the kind of result people act on: budgets move to the campaigns at the top.

The OWOX Data Marts extension for Google Sheets covers that half. A data analyst publishes a data mart (a table or an SQL query in your data warehouse) and makes it available for reports. In Sheets you open the extension, pick the data mart, tick the columns you want and run it. The rows land in a tab, ready for your formulas.

Google Sheets with the OWOX Data Marts extension open beside an ad spend table: the Unified Ad Spend data mart, nine of fourteen columns ticked and a Save & Run button – the rows in the sheet come from a governed data mart, not a pasted export

What changes for the person who maintains the sheet:

  • Refresh replaces re-export. Refresh current report in the extension menu re-runs the same pull, and a scheduled refresh keeps the tab current without anyone touching it.
  • The source is written on the sheet. The header cell of an imported tab carries a note with the data mart it came from, the time of the import and a link to the report.
  • No SQL and no warehouse access needed. The analyst decides which data marts and columns are available. You choose from that list.

The extension does not write or change the analyst’s SQL. SORT and SORTN then rank the complete, current rows on every refresh. The extension comes with the OWOX Cloud plans of OWOX Data Marts; spreadsheet reporting describes both ways data reaches a sheet.

Your New Normal

Turn your data into decisions.

Governed data marts give you the clean foundation ML needs to actually work.

  • No AI hallucinations
  • Analyst-governed definitions
  • Every number traces to SQL
Get started free

FAQ

Frequently Asked Questions

How can I sort data from another tab in Google Sheets using the SORT function?

To sort data from another tab, reference the tab name in the SORT function. For example, =SORT(Sheet2!A2:C10, 2, TRUE) sorts data from Sheet2, using the second column in ascending order. Replace Sheet2 with the desired tab name and adjust the range and criteria as needed.

What should I do if I encounter a #VALUE! error while using the SORT function?

Check the sort column first: its number is counted from the left edge of the range, so in B3:D10 column D is 3, and a number larger than the range has columns returns an error. A sort range must have the same number of rows as the data. If the error is #REF! with 'Array result was not expanded', the cells where the result should go are not empty.

How do I remove duplicates when sorting data in Google Sheets?

To remove duplicates when sorting data, combine the SORT and UNIQUE functions. For example, use =SORT(UNIQUE(A2:C10), 1, TRUE) to first remove duplicates from the range A2:C10 and then sort the data by the first column in ascending order. This ensures unique, organized results.

Can I sort data by multiple columns in Google Sheets?

Yes, you can sort data by multiple columns using the SORT function. Specify the range and sorting criteria for each column. For example, =SORT(A2:C10, 1, TRUE, 2, FALSE) sorts by the first column in ascending order, then by the second column in descending order, layering sorting logic.

How do I sort data in ascending or descending order using the SORT function?

Use the SORT function to organize data in ascending or descending order by specifying the range, column, and order. For example, =SORT(A2:C10, 2, TRUE) sorts the data by the second column in ascending order. Replace TRUE with FALSE for descending order. This simplifies dynamic data organization.

What is the difference between the SORT and SORTN functions in Google Sheets?

SORT returns all rows of the range in sorted order. SORTN returns only the first n rows of the sorted result and has four modes for ties: 0 returns exactly n rows, 1 adds the rows tied with the last one, 2 removes duplicates first, and 3 returns n distinct values with all their duplicates. Without a sort column SORTN sorts by the first column in ascending order, so always name the column and the direction.

Who wrote this

Vadym Kramarenko

Vadym Kramarenko · Growth Marketing Manager

Vadym Kramarenko is a Growth Marketing Manager at OWOX, where he drives user acquisition and product-led growth strategies. He hosts the OWOX podcast, interviewing analytics professionals about data-driven marketing, attribution, and reporting best practices. Vadym specializes in turning complex analytics concepts into practical, actionable marketing frameworks.