SORTBY Function
Sorts an array using other arrays as keys.
Syntax
Parameters
| Parameter | Description | Required |
|---|---|---|
| array | The range or array to be sorted (returned in the result). | Required |
| by_array1 | The first array of values to sort by; must have dimensions compatible with array. | Required |
| sort_order1 | 1 for ascending (default) or -1 for descending for the first key. | Optional |
| by_array2 | An optional additional array to sort by when the first key ties. | Optional |
| sort_order2 | Sort 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)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)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)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.
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
SORTBY
sortbyPractice 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.
File format: CSV (comma-separated values) - opens in Excel, Google Sheets, and all spreadsheet apps