Quick Answer

SORTBY Function

Sorts an array using other arrays as keys.

✓ Excel✓ Google SheetsExcel Microsoft 365

Syntax

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

Parameters

ParameterDescriptionRequired
arrayThe range or array to be sorted (returned in the result).Required
by_array1The first array of values to sort by; must have dimensions compatible with array.Required
sort_order11 for ascending (default) or -1 for descending for the first key.Optional
by_array2An optional additional array to sort by when the first key ties.Optional
sort_order2Sort order for the second key (1 or -1).Optional

Basic Example

Sort one column using another as the key

=SORTBY(B2:B10, A2:A10, 1)
ResultValues of B2:B10 reordered to match ascending order of A2:A10

SORTBY orders column B by the values in column A ascending.

Advanced Examples

Example 1: Sort by multiple keys

Sort names by Department ascending, then by Score descending.

Supply two by-arrays and orders

=SORTBY(C2:C20, A2:A20, 1, B2:B20, -1)
Result: Names ordered by department, then by score highest-first within each department
The first by_array sets the primary order and the second breaks ties.

Example 2: Descending by a helper column

Sort a list newest-first using a date column.

Use -1 for descending

=SORTBY(A2:A10, B2:B10, -1)
Result: Items in A2:A10 ordered by dates in B2:B10, newest first
sort_order -1 sorts the helper column descending.

How SORTBY Works

SORTBY sorts array according to the values in one or more separate by_arrays. Each by_array has an optional sort_order of 1 (ascending) or -1 (descending); additional by_array/order pairs act as tie-breakers. The by_arrays must share the same height/width as array. The result is a dynamic array that spills into neighboring cells.

1
Pick the result array
Choose the range you want returned.
2
Add the key arrays
Supply one or more by_arrays and their orders.
3
Ensure spill space
Leave empty cells for the spilled result.

Important Notes & Limitations

  • Microsoft 365 only.

  • Each by_array must have the same height/width as array.

  • Cannot sort by a computed expression unless it resolves to an array.

Common Errors & Fixes

#SPILL!The spill range is blocked by other content.

Fix: Clear the cells in the spill area.

#VALUE! (array size mismatch)A by_array has different dimensions than array.

Fix: Make all by_arrays the same size as the array argument.

Download Practice File

Practice SORTBY with Real Data

Download a sample CSV file with pre-populated data and practice exercises for the SORTBY function. Works in both Excel and Google Sheets.

Works in Google SheetsCompatible with ExcelIncludes exercises

File format: CSV (comma-separated values) - opens in Excel, Google Sheets, and all spreadsheet apps

Frequently Asked Questions

SORTBY vs SORT?
SORTBY sorts by other arrays; SORT sorts the array by its own columns/rows.
Is SORTBY in Google Sheets?
No native SORTBY; Sheets uses SORT with index/order arguments.
Can I use multiple sort keys?
Yes, supply additional by_array and sort_order pairs.