Sort a list with SORT and SORTBY
Formulas that spill8 minPremium
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( 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( 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)- Step 1: The 2 picks the second column of
A2:B9, the units on hand. - Step 2: Smallest comes first, so bubble wrap, with 9 units, moves to the top.
- Step 3: Whole rows move together, and labels, with the most units, end up last.
Common mistakes
=SORT(takes in the header row, and the header lands in the sorted list. Start the range below the headers.A1:B9, 2, -1) =SORT(sorts the numbers on their own, so they lose their names. Sort whole rows, or name the column to sort by in SORTBY.B2:B9, 1, -1) - In
=SORT(, the 3 counts from B, so it sorts by column D, the third column of the array.B2:D9, 3) =SORTBY(gives ranges of different heights and showsA2:A9, B2:B10) #VALUE!. Give both the same rows.
In short
=SORT(sorts whole rows by one of their columns, largest first.array, column number, - 1) - The column number counts from the left edge of the array, not of the sheet.
=SORTBY(sorts by ranges that needn't be in the result, one level after another.array, range, order, ...) - The original list keeps its order, and the sorted copy keeps up with it.
Practice
Premium4 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.
Have one? Sign in