Introduction
The SORTBY function sorts a range or array using values from corresponding sort keys. It returns the selected array with each row's values kept together, which makes it useful when you want to order a table by a different column.
Syntax
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)Depending on your regional settings, Excel may use semicolons (;) between arguments.
Arguments
- array — The range or array to sort. Required.
- by_array1 — The first key range or array whose values determine the order. Required.
- sort_order1 — Optional. Use
1for ascending or-1for descending. If omitted, ascending order is used. - by_array2, sort_order2, ... — Optional additional key and sort-order pairs for multi-level sorting. Each sort order can be
1or-1.
Excel Versions
Microsoft lists SORTBY for Excel for Microsoft 365, Excel for the web, Excel 2024, and Excel 2021, including Mac editions. The function page also lists Excel for iPad, iPhone, and Android. Versions older than those listed are not named on the function page.
Examples
Enter this data in A1:C5:
| Product | Sales | Region |
|---|---|---|
| Tea | 120 | West |
| Coffee | 180 | East |
| Milk | 150 | West |
| Bread | 180 | East |
- Sort sales from low to high. Enter
=SORTBY(A2:C5,B2:B5,1)in an empty area. The rows are ordered by sales values 120, 150, 180, and 180. Coffee and Bread are tied; this formula does not specify a secondary order for them. - Sort sales from high to low. Use
=SORTBY(A2:C5,B2:B5,-1). Rows with sales of 180 appear first, followed by 150 and 120, with each row's values kept together. - Sort by region, then sales descending. Use
=SORTBY(A2:C5,C2:C5,1,B2:B5,-1). Regions are ordered A–Z; within each region, the highest sales appear first. - Sort product names A–Z. Use
=SORTBY(A2:C5,A2:A5,1). The full rows move according to the names in the first column.
The formula spills from its cell, so place it in a clear area outside the source table.
Tips
- Use
SORTBYwhen a key column controls the order but you want to return several columns together. - Add more key and sort-order pairs to set second, third, and later sort priorities when earlier keys tie.
- Use
-1for descending order and1for ascending; ascending is the default if the order argument is omitted. - If your data is in an Excel Table, structured references can help the referenced range adjust as rows are added or removed.
- Leave enough room for the spilled result. Microsoft notes that dynamic array links between workbooks have limitations; for the described scenario, the source workbook must remain open when the link is refreshed.
Common Errors
#VALUE!for the sort order: Use only1or-1for each sort-order argument.- A key does not align with the data: Make sure each key array aligns with the dimension being sorted—row counts when sorting rows, or column counts when sorting columns.
#SPILL!: Clear cells that block the output range.- Unexpected order: Check that the keys and their
1or-1directions are in the intended argument order. The first key has the highest priority. #REF!with a source in another workbook: For dynamic array links between workbooks, keep the source workbook open when the formula is calculated or refreshed.
Compatibility
SORTBY returns a dynamic array that can spill. Microsoft lists the function for Microsoft 365, Excel for the web, Excel 2024, and Excel 2021, as well as the mobile apps named on the function page. Check the target users' Excel versions before sharing a workbook with older editions. In some locales, use semicolons instead of commas as argument separators.
Source: SORTBY function — Microsoft Support (accessed December 4, 2026).
Conclusion
SORTBY can order an entire table by one or more keys while preserving the relationship among values in each row. Try the multi-level example on a copy of your data.