Lookup & Reference

SORTBY Function in Excel

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

Syntax

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

Arguments

  • array (required): The range or array to sort
  • by_array1 (required): The array to sort by
  • sort_order1 (optional): 1=ascending, -1=descending

Examples

  • =SORTBY(A2:B10, C2:C10, -1) - Sort A:B by values in C descending - Result: Sorted array

When to use SORTBY

  • Use SORTBY when you need to retrieve values from a table or range based on a key.
  • Confirm data types match (text vs number) and trim spaces before lookups.
  • Pair with [IFERROR](/functions/iferror/) or [IFNA](/functions/ifna/) for cleaner reports.
  • See the [lookup functions guide](/guides/excel-lookup-functions-guide/) for VLOOKUP, XLOOKUP, and INDEX MATCH paths.

Common SORTBY mistakes

  • #N/A often means the key is missing or exact match is wrong — see [#N/A error](/errors/na/).
  • Lock table references with $ when copying formulas down a column.
  • Compare legacy vs modern lookups in [XLOOKUP vs VLOOKUP](/compare/xlookup-vs-vlookup/).

Common errors

  • by_array must have same number of rows as array

Use cases

  • Sort by external criteria
  • Multi-level sorting
  • Complex rankings

Frequently asked questions

  • What's the difference between SORT and SORTBY? SORT sorts by a column within the array itself (by index number). SORTBY sorts by any external array/column, even one not in the output. SORTBY also supports multiple sort levels natively: =SORTBY(A2:D10, B2:B10, 1, C2:C10, -1) sorts by B ascending, then C descending.
  • How do I sort by a custom order (not alphabetical)? Create a helper column with custom sort numbers, or use MATCH: =SORTBY(A2:B10, MATCH(A2:A10, {"High","Medium","Low"}, 0), 1). This sorts by your custom order (High first, then Medium, then Low) instead of alphabetically.
  • Can I sort by multiple columns with SORTBY? Yes! Add more by_array and sort_order pairs: =SORTBY(A2:E10, B2:B10, 1, D2:D10, -1, C2:C10, 1) sorts by column B ascending, then D descending, then C ascending. There's no practical limit to sort levels.

Editorial review

  • Reviewed by Excel.Directory Editorial Team. Updated May 2026.