Excel SORTBY Function

SORTBY returns a dynamically sorted copy of a range or array using values from one or more corresponding ranges.

Use it to order names by scores, products by sales, tasks by due dates, or entire tables by columns that may sit outside the returned range.

1
Choose what to return The range that will be reordered
2
Choose what controls the order A matching row or column of sort keys
3
Spill the sorted result Extra sort ranges can break ties

SORTBY syntax & arguments

Syntax

=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
Required Optional
  1. 1

    array

    Required

    The range or array containing the values to return in sorted order.

  2. 2

    by_array1

    Required

    The corresponding row or column whose values determine the primary sort order.

  3. 3

    sort_order1

    Optional

    Controls the primary sort order:

    • 1 — Ascending: Sorts from smallest to largest or A to Z. This is the default.
    • -1 — Descending: Sorts from largest to smallest or Z to A.
  4. 4

    by_array2, sort_order2

    Optional Repeatable

    Additional sort ranges and their orders. Excel uses each pair to break ties left by the earlier sort ranges; omit an order to use ascending order.

Example

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

Sort rows in A2:C20 by the corresponding values in C2:C20 from largest to smallest.

SORTBY caveats

  • Sort ranges must line up

    Each by_array must be one row high or one column wide and align with the dimension being sorted. Mismatched sizes return a #VALUE! error.

  • The result needs room to spill

    SORTBY returns a separate dynamic array and leaves the source data unchanged. A blocked output range causes a #SPILL! error.

  • Later sort ranges only break ties

    SORTBY applies sort ranges from left to right. A second by_array only controls rows whose first sort values are equal.

  • Only two sort orders are valid

    Use 1 for ascending order or -1 for descending order. Any other value returns a #VALUE! error, while an omitted order defaults to ascending.

Keeping the sort simple? When the column controlling the order is already part of the returned array and you only need one sort level, SORT is usually the clearer choice. Use SORTBY when the sort range sits elsewhere or when additional sort levels need to break ties.

Intro SORTBY practice problem

Solve the intro problem directly here, or open it on its own page.

Open full problem

Advanced SORTBY practice problems

Use SORTBY alongside other Excel functions in realistic, less-prescriptive challenges.

No advanced SORTBY problems are currently available.