What is the SORT function?
=SORT(array, [sort_index], [sort_order], [by_col])SORT returns a sorted copy of a range. Unlike the Sort button, it leaves the original table alone and builds the sorted version elsewhere. When the source values change, the order updates right away.
The result is an array the same size as the original, spilling from the formula cell. Per Microsoft documentation, it is available in Excel 2021 and later and Microsoft 365.
Arguments
- array
- The range to sort.
- sort_index
- Optional. Which column of the array (or row, with by_col) to sort by. Omitted means 1.
- sort_order
- Optional. 1 for ascending (smallest first), -1 for descending (largest first). Omitted means 1.
- by_col
- Optional. FALSE (default) sorts rows top to bottom; TRUE sorts columns left to right.
SORT examples
Example 1. First column, ascending
=SORT(A2:D9)Rows sorted by branch name.
Example 2. Largest sales first
=SORT(A2:D9, 4, -1)Sorted by the 4th column (Sales), largest first.
Example 3. Sort one column
=SORT(B2:B9)A sorted list of product names.
Example 4. Sorted, no duplicates
=SORT(UNIQUE(B2:B9))Each product name once, sorted.
Common mistakes
Counting sheet columns for sort_index
sort_index is the position inside the array. In B2:D9, column D is column 3. A number outside the array gives #VALUE!.
Sorting the header too
If the array includes the header row, the header is mixed in like data. Select the data rows only.
The spill range is blocked
If cells the result would spill into already hold values, you get #SPILL!.
Inserting a column shifts the key
Because the key is a column number, inserting or deleting columns inside the array can sort by a different column. Microsoft documentation recommends SORTBY, which uses a range, in that case.
FAQ
- What order do tied rows get?
- The calculation on this page keeps their original order. To make the order certain, give SORTBY a second key.
- How do I sort by two keys?
- You can pass arrays for the column and order, like =SORT(A2:D9, {1,4}, {1,-1}). If the key is outside the table, SORTBY is easier.
- How do I sort the original table?
- SORT makes a copy. To change the table itself, use Sort on the Data tab.