SORT, SORTBY and UNIQUE in Numbers: Sort and Deduplicate
The examples use this table.
| A | B | C | |
|---|---|---|---|
| 1 | Product | Price | Qty |
| 2 | Apple | 4 | 10 |
| 3 | Pear | 6 | 5 |
| 4 | Kiwi | 12 | 2 |
| 5 | Grape | 8 | 3 |
Sort a list
SORT returns a sorted copy of a range and leaves the original untouched. Wrapped in TEXTJOIN, you can see the sorted names in one cell.
=TEXTJOIN(", ",TRUE,SORT(A2:A5))Result: Apple, Grape, Kiwi, Pear
Sort from high to low
The third argument is the sort order: 1 for ascending and -1 for descending. The second argument is the column to sort by. Here the prices are sorted from high to low and the first value is taken with INDEX.
=INDEX(SORT(B2:B5,1,-1),1)Result: 12
Sort one range by another
SORTBY sorts a range by the values in a different range. You can sort product names by price without showing the prices themselves.
=TEXTJOIN(", ",TRUE,SORTBY(A2:A5,B2:B5))Result: Apple, Pear, Grape, Kiwi
Remove duplicates
UNIQUE returns each value only once. Here two lists are placed under each other with VSTACK, which gives six rows with two repeats. UNIQUE keeps four.
=ROWS(UNIQUE(VSTACK(A2:A5,A2:A3)))Result: 4
Turn a column into a row
TRANSPOSE flips rows and columns. The four products in a column become four columns in a row.
=COLUMNS(TRANSPOSE(A2:A5))Result: 4
Frequently asked questions
How do I sort in descending order with SORT?
Use -1 as the third argument, for example =SORT(B2:B5,1,-1).
How do I remove duplicates in Numbers with a formula?
Use UNIQUE, for example =UNIQUE(A2:A20). It returns every value once.
What is the difference between SORT and SORTBY?
SORT sorts a range by one of its own columns. SORTBY sorts a range by the values in another range of the same size.
Functions in this article
Practise it yourself
Type the formula yourself in a real exercise and see right away if your answer is correct. The first exercises are free.
Start practising