Array

SORTBY Formula

Sorts an array based on the values in one or more separate arrays or columns, with independent sort order for each. SORTBY is more flexible than SORT because the column you sort by doesn't have to be in the returned array, and you can easily sort by multiple criteria.

Syntax

SORTBY(array, by_array1, [sort_order1], ...)
ParameterDescription
array Parameter of the SORTBY function.
by_array1 Parameter of the SORTBY function.
[sort_order1] (Optional.) Parameter of the SORTBY function.
... Parameter of the SORTBY function.
Try SORTBY in Viztab — free, no signup

Examples

Sort names by scores

Formula
=SORTBY(A2:A20, B2:B20, -1)
Returns the names from column A sorted by the scores in column B, highest first. The scores column doesn't need to be in the output.

Multi-level sort

Formula
=SORTBY(A2:C50, B2:B50, 1, C2:C50, -1)
Sorts by column B ascending first, then by column C descending as the tiebreaker. Equivalent to a two-level sort in the Sort dialog.

Sort products by calculated margin

Formula
=SORTBY(A2:D30, (D2:D30-C2:C30)/C2:C30, -1)
Sorts products by profit margin (calculated from price minus cost / cost) without needing a helper column.

Common Errors

#VALUE!

The sort-by array has a different number of rows than the main array.

#SPILL!

The spill range contains non-empty cells that block the output.

Tips

Sort by a column not in the output

SORTBY's biggest advantage: you can return columns A:C but sort by column E without including E in the output. SORT can't do this.

Sort by calculations

The by_array can be a formula, not just a range. Sort by LEN(A2:A20) to sort by text length, or by MONTH(D2:D20) to sort by month.

Multiple sort levels

Add as many by_array/sort_order pairs as needed. Each pair after the first acts as a tiebreaker for the previous sort level.

Try SORTBY in Viztab

Import your data and use SORTBY with 370+ other formulas. No signup required.

Open Viztab

Related Formulas