Skip to content

Sort a list with SORT and SORTBY

Formulas that spill8 minPremium

Builds on Fill a range with one formula, and fix #SPILL!.

A sales manager in Hamburg keeps a leaderboard on the team's screen. The sales log lists the reps in the order they joined, and the board needs the biggest seller on top, sorted again every time a new figure comes in.

How it works

SORT(array, [sort_index], [sort_order]) returns the rows of array in order of one of its columns. sort_index counts the columns of array from its left edge, so 2 means its second column, wherever that sits on the sheet. sort_order is 1 for smallest first, the default, or -1 for largest first. Text sorts A to Z, whatever its capitals.

SORTBY(array, by_array1, [sort_order1], ...) sorts by ranges you name, and they don't have to be part of the result. That's how you list names in order of sales without showing the sales. Add another pair to sort inside the first: by region, then by sales within each region.

Think of both as a second, sorted copy of the same rows. The original keeps its order, and the copy sorts itself again whenever a value changes, which Data > Sort can't do.

Example

A warehouse lists its packing supplies with the units on hand in B2:B9.

=SORT(A2:B9,2)
Returns the rows of A2:B9 sorted by their second column, the units on hand, smallest first.

  1. Step 1: The 2 picks the second column of A2:B9, the units on hand.
  2. Step 2: Smallest comes first, so bubble wrap, with 9 units, moves to the top.
  3. Step 3: Whole rows move together, and labels, with the most units, end up last.
The 2 picks the second column of the array, and whole rows move, so each product keeps its own count. In D2, =SORT(A2:B9,2) returns Bubble wrap.

Common mistakes

  • =SORT(A1:B9,2,-1) takes in the header row, and the header lands in the sorted list. Start the range below the headers.
  • =SORT(B2:B9,1,-1) sorts the numbers on their own, so they lose their names. Sort whole rows, or name the column to sort by in SORTBY.
  • In =SORT(B2:D9,3), the 3 counts from B, so it sorts by column D, the third column of the array.
  • =SORTBY(A2:A9,B2:B10) gives ranges of different heights and shows #VALUE!. Give both the same rows.

In short

  • =SORT(array, column number, -1) sorts whole rows by one of their columns, largest first.
  • The column number counts from the left edge of the array, not of the sheet.
  • =SORTBY(array, range, order, ...) sorts by ranges that needn't be in the result, one level after another.
  • The original list keeps its order, and the sorted copy keeps up with it.

Practice

Premium

4 exercises: 3 formulas and 1 quiz question.

Practice is part of Premium

Every lesson is free to read, and you can practice the free lessons without an account. This lesson's exercises are part of Premium, which you add to a free account.