Numbers Exercises

SORT, SORTBY and UNIQUE in Numbers: Sort and Deduplicate

Last updated:

Short answer: SORT puts values in order, SORTBY sorts one range by another, and UNIQUE removes duplicates. For example =TEXTJOIN(", ",TRUE,SORT(A2:A5)) lists the products alphabetically.

The examples use this table.

Table 1
ABC
1ProductPriceQty
2Apple410
3Pear65
4Kiwi122
5Grape83

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