Skip to content
ExcelExcel 2021+

SORTBY function in Excel

Sorts the contents of a range or array based on the values in a corresponding range or array

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

SORTBY syntax and parameters

Five arguments, two required.

  • arrayarrayRequired

    The array or range to sort

  • by_array1arrayRequired

    The array or range to sort on

  • sort_order1numberOptional

    The order to use for sorting. 1 for ascending, -1 for descending. Default is ascending.

  • by_array2arrayOptional

    The array or range to sort on

  • sort_order2numberRepeating

    The order to use for sorting. 1 for ascending, -1 for descending. Default is ascending.

SORTBY examples

Formulas you'll actually reuse.

  1. Deals largest first — the sort key doesn't have to be inside the returned range:

    =SORTBY(A2:C200, C2:C200, -1)
  2. By region A→Z, then by amount descending within each region:

    =SORTBY(A2:C200, B2:B200, 1, C2:C200, -1)

SORTBY in Google Sheets

No direct equivalent — here's the way around.

Try SORTBY in the playground

Edit the example — nothing to install.

Preloaded with the SORTBY formula from Example 1 — change anything and watch it respond.

Loading the editor…

SORTBY errors

What they mean — and the fixes.

  • #VALUE!

    A by_array isn't the same height (or width) as the array being sorted.