The SORT function is a dynamic array function that returns a sorted version of a range or array. Unlike manual sorting, it doesn't alter your original data. Instead, it outputs a new sorted array in a separate location and updates automatically whenever the source data changes. This makes it the most efficient way to sort data in Excel using a formula.
Key characteristics of the SORT function:
If you're using an older version of Excel that doesn't support the SORT function, this article also covers a legacy formula approach using COUNTIF, INDEX, MATCH and ROWS.
SORT Function Syntax
The syntax for the SORT function is:
| Argument | Required? | Description | Default |
|---|---|---|---|
| array | Required | The range or array to sort | — |
| sort_index | Optional | The column or row number to sort by | 1 |
| sort_order | Optional | 1 for ascending, -1 for descending | 1 (ascending) |
| by_col | Optional | FALSE to sort by row, TRUE to sort by column | FALSE (by row) |
For example, =SORT(A2:D11, 4, -1) sorts the range A2:D11 by the fourth column in descending order.
The SORT function is not available in every version of Excel. Before building your formula, confirm your version supports it.
Supported versions:
Unsupported versions:
If you're working in an unsupported version, skip ahead to the Sort Dynamic Data section below, which walks through a workaround formula using COUNTIF, INDEX and MATCH.
To quickly sort or filter your data for one-time analysis, use Excel's built-in sort and filter tools:
For example, if you uncheck the Select All box and check the box(es) for the 1123 Store number in the Store column, you can see only those sales that came from that store.
This can provide you many ways to sort and view your data to help you make sense out of it. You can also apply conditional formatting to visually highlight key values as you sort and filter. But what if your data changes frequently?
If you don't have access to the SORT function, you can build a formula using COUNTIF, INDEX, MATCH and ROWS that sorts your data automatically. As with lots of Excel tips and tricks of this nature, what you gain in analysis you might lose in convenience. Or, in simplicity at least.
Let’s look at one way to sort dynamic information and then explore the limitations of the results!
Our example will allow us to take a table of sales figures and generate a sorted list that shows us our top performers for the quarter. We could sort our report by Quarter Totals (Column E), but then the rest of our year would be out of order. We could only see sales ranking one quarter at a time. Because we receive our source data alphabetically by salesperson, we would also have to re-sort by salesperson each time we add another quarter.
This is what our table might look like as the year progresses…
Instead, we will use the COUNTIF, INDEX, MATCH, and ROWS functions to create a formula that will sort our sales figures and display them in a new table. This takes two steps.
We will first generate a 1-10 numerical ranking of the information we want sorted. This will not "rearrange" the information, but tell us its place in the list. The formula that goes in G2 is:
=COUNTIF(E$2:E$11,"<="&$E2)
Enter the formula, then drag to copy it into each cell in the column. The result should be a number from 1-10 in each row.
As we learned in Using the INDEX function in an Excel Formula, INDEX tells Excel to get a value in a specific row and column. We will use it here to help us match the correct rank with the correct sales total, and then return the list in ascending order. The sort formula we will put in H2 is:
=INDEX($E$2:$E$11,MATCH(ROWS($G$2:G2),$G$2:$G$11,0))
If we want to also display the names of the salespeople next to the sorted sales totals, we would simply add the same formula to the next column, but change the INDEX function to return values from the Salesperson column:
=INDEX($A$2:$A$11,MATCH(ROWS($G$2:G2),$G$2:$G$11,0))
Before we get too excited about this method for sorting data using an Excel function, there's a catch. Sadly, these formulas will NOT work if:
But, if your data is rarely likely to generate duplicates and is very consistent such as the above example, this technique can deliver a lot of value. If your data is more complex in the above ways, you will at least have a starting place to build a sort formula that can work with the exceptions.
If you have access to Excel 365 or Excel 2021, you have two powerful dynamic array functions for sorting: SORT and SORTBY. They serve different purposes.
The SORT function sorts an array by a column (or row) within that same array. You specify the column number to sort by. This works well when the sort key is part of the data you want returned.
The SORTBY function sorts an array based on a separate range or array. Its syntax is:
=SORTBY(array, by_array1, [sort_order1], [by_array2], [sort_order2], ...)
Key differences between the two:
For example, if you have employee names in column A and performance scores in column B, and you only want to return the sorted names, =SORTBY(A2:A11, B2:B11, -1) returns names sorted by score in descending order without displaying the scores.
If your Excel sort formula isn't working, check for these common issues:
Double-check your cell references and make sure you're using absolute references (with the $ symbol) where needed so your formulas don't break when copied across rows.
Formula-based sorting is a powerful skill for anyone who works with data that changes regularly. Whether you're using the SORT function in Excel 365 or building legacy formulas in an older version, mastering these techniques saves time and reduces errors in your daily workflow.
Pryor Learning offers hands-on Excel training courses designed for every skill level, from spreadsheet basics to advanced formulas and data analysis. Explore a PryorPlus subscription for unlimited access to On-Demand courses, or join a live seminar for guided, interactive learning.