Back to functions
Lookup & Reference2026-06-130 related articles

SORTBY Function in Excel

Sort a range or array based on values in a separate range or array.

Syntax

SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)

Arguments

array

Required

The range or array that you want to return in sorted order.

by_array1

Required

The first range or array Excel should use as the sort key.

sort_order1

Optional

Use 1 for ascending order or -1 for descending order. The default is ascending.

by_array2

Optional

An additional range or array to use as another sort key when the first one is tied.

What it returns

Returns a sorted dynamic array based on one or more linked sort arrays.

What SORTBY does

SORTBY sorts one range by using values from another range. That makes it useful when the values you want to display and the values you want to sort by are not in the same selected block.

Practical examples

Sort names by age

=SORTBY(D2:E9,E2:E9)

This returns the names and ages from D2:E9, sorted by the ages in E2:E9.

Sort by one helper column in descending order

=SORTBY(A2:C20,C2:C20,-1)

This keeps the original three-column output together while sorting by column C from highest to lowest.

Common mistakes and notes

SORTBY uses linked sort arrays

The by_array arguments must line up with the main array. If the shapes do not match, Excel cannot sort the result correctly.

Spill behavior still applies

Like other dynamic array functions, SORTBY needs room to spill the full result. Blocked cells can produce #SPILL!.

Use multiple sort keys when ties matter

If one sort key is not enough, add another by_array and sort_order pair instead of trying to nest extra sorting logic.

Related functions

Official documentation