Skip to content

How to Sort Data in Excel

    In this guide, we’ll look at how to sort data in Excel, focusing specifically on more complex scenarios where we want to apply multiple conditions.

    We’ll use data from the 2024–2025 NBA regular season to sort by:

    • Teams ranked from most successful to least successful
    • Teams ranked from most successful to least successful, then sorted by net rating (difference between points scored and points allowed) and team name

    The examples use numbers and text, but keep in mind that if you have date values (since dates are treated as numbers by Excel), the process will be exactly the same as described in this guide. If you want to follow the guide step by step, download the file!

    We’ll explore three solutions, including Excel’s built-in tools, formulas, and Power Query. Let’s get started!

    How to Sort Data in Excel Using One or More Criteria

    Table filter menu open on the WINS column with sort and filter options

    You can sort Excel data in several ways, but the most straightforward is to use Excel’s standard sorting feature. The shortcut to bring up the options, after selecting your data headers, is CTRL+SHIFT+L, which is useful if you need to sort or filter your data.

    As you’ll notice, from this menu you can sort A–Z (ascending), Z–A (descending), and even by color. If you only need a single condition, these options should be more than enough.

    If you have multiple criteria, you’ll need custom sorting. To access the custom sorting options, which we’ll cover shortly, you should select Sort by Color > Custom Sort. This process isn’t exactly intuitive, so let’s take a closer look at an alternative.

    Home tab Sort & Filter menu with Custom Sort highlighted

    Specifically, if you want to sort by multiple criteria, you should go to Home > Sort and Filter > Custom Sort, so you can select multiple columns as criteria.

    Sort dialog with three levels: WINS and NET_RATING largest to smallest, then TEAM A to Z

    There’s a hierarchical order among the individual levels, as you can see:

    • The first sort is by wins, from highest to lowest
    • The second column shows the net rating, sorted from largest to smallest
    • The third is based on team name, sorted from A to Z, since it is a text value
    NBA table sorted by wins, net rating and team name after the custom sort

    The output shows the data sorted, with the Oklahoma City Thunder standing out, along with four teams with 50 wins. The Clippers, Nuggets, Pacers, and Lakers are ordered this way because, although they each have 50 wins, the Clippers have the highest net rating (4.8), followed by the Nuggets (3.9), the Pacers (2.2), and finally the Lakers (1.2).

    The problem with this method is that it isn’t permanent. If I sort the data differently, I’ll lose the previous sort order. There’s therefore a certain degree of risk, especially if multiple people are working on the file. The same applies to PivotTables, where we can sort the data as we like, but the next person’s selection overwrites ours, which could cause problems during analysis.

    If, on the other hand, we work with formulas, we can create an array to get the data exactly as we want it, regardless of the previous sort order. Let’s see how to do it.

    How to Sort Data in Excel with One or More Conditions Using Formulas

    Before dynamic arrays, it wasn’t that easy to sort data in Excel based on one or more conditions using built-in functions. Fortunately, the SORT function comes to the rescue:

    SORT formula returning the teams sorted by wins from largest to smallest

    =SORT(TBL_NBA[[TEAM]:[WINS]],2,-1)

    There are three key elements in this formula:

    • TBL_NBA[[TEAM]:[WINS]] selects all columns from TEAM to WINS. If you work with tables, this shouldn’t surprise you; in this case, it’s convenient because there are only two consecutive columns
    • 2 gives priority to the second column in the reference, i.e., WINS
    • -1 sorts the data in descending order, from largest to smallest

    We could have achieved the same result with SORTBY, which I’m including just for the record:

    =SORTBY(TBL_NBA[[TEAM]:[WINS]],TBL_NBA[WINS],-1)

    The key difference between the two functions is that with SORTBY, we can specify the column we’re interested in, so it’s clearer to see how “wins” actually serves as our guide, with the values always in descending order (-1).

    How do these formulas change if there’s more than one condition? Let’s take a look together by replicating the previous example with wins (Z–A), rating (Z–A), and team (A–Z):

    SORT formula with several sort conditions applied to the NBA table

    =SORT(HSTACK(TBL_NBA[TEAM],TBL_NBA[WINS],TBL_NBA[NET_RATING]),
    {2,3,1},
    {-1,-1,1})

    This formula is definitely more complex than the previous one; let’s break it down step by step:

    • HSTACK(....) is combining the three columns: TEAM, WINS, and NET_RATING. These are the three columns that will make up our final array
    • {2,3,1} indicates the order of priority for these columns. First the second (WINS), then the third (NET_RATING), then the first (TEAM).
    • {-1,-1,1} refers to the three preceding columns. The first -1 is linked to the first column of the preceding argument (WINS, the 2 in {2,3,1}), so the first condition is: sort the wins (second column of the final array) in descending order (Z-A). The second -1 is in the second position, so it corresponds to the previous 3, i.e., NET_RATING. Here we specify that the rating should also be sorted in descending order (Z-A), and this condition applies only when one or more teams have the same number of wins. The third condition sorts the team, that is, the third column from the previous one, in ascending order (1)

    Let’s say, for simplicity’s sake, that the two arrays are linked by position as the key: {A,X,C} and {1,-1,-1} would mean that A would be sorted in ascending order (A and 1), while X would be in descending order (X and -1) and C likewise (C and -1).

    Some functions have simplified this step; for example, in GROUPBY and PIVOTBY, you’d use -2, which would indicate, in an array, to sort in descending order based on the second column (2 and -1). With SORT, unfortunately, we cannot do this automatically.

    SORTBY formula with HSTACK sorting by wins, net rating and team name

    Easier to understand, though less concise, is the solution using SORTBY, which follows the same logic as before:

    =SORTBY(HSTACK(TBL_NBA[TEAM],TBL_NBA[WINS],TBL_NBA[NET_RATING]),
    TBL_NBA[WINS],-1,
    TBL_NBA[NET_RATING],-1,
    TBL_NBA[TEAM],1)

    Instead of creating two arrays, we’re specifying the three conditions one after another, consecutively. In my work as a consultant, I use SORT more often because it’s more intuitive when working with arrays, but there’s nothing wrong with using SORTBY as well, the output will be the same regardless of which function is used.

    As a general rule, if you need to sort data using formulas, SORT and SORTBY are the standard options you’re looking for, unless there are already dedicated arguments, as in GROUPBY or PIVOTBY.

    How to Sort Data in Power Query in Excel

    Power Query editor with the WINS column filter menu and sort options open

    Now let’s see how to sort data in Power Query in Excel by applying multiple conditions. Load the table into Power Query and then sort it by simply clicking on the relevant column, in this case, WINS. You can then select the arrow on the right to choose how to filter or sort the data.

    Power Query Sorted Rows step sorting the NBA teams by wins in descending order

    If we sort by a single condition, the code is simple because it highlights a pair of values: {"WINS", Order.Descending}. We’re sorting the data by wins, from the team with the most wins to the one with the fewest.

    Power Query Sorted Rows step sorting by wins and then by net rating

    If we repeat the process with the NET_RATING column, we’ll notice that we now have another pair of values specific to NET_RATING: {"NET_RATING", Order.Descending}. Here, too, we sort the rating data for each team in descending order. As you’ll notice, however, there’s a #1 next to WINS and a #2 next to NET_RATING, which indicates the hierarchy among the various conditions.

    Power Query Sorted Rows step sorting by wins, net rating and team name

    Let’s also add the third condition, and you won’t be surprised to see the third pair of values, where this time TEAM is in ascending order.

    Detail of the Power Query Table.Sort formula and the sorted NBA teams

    Once the process is complete, you’ll notice that the three criteria are all in the same step in Power Query, numbered 1 through 3. Here’s the code:

    let
    Source = Excel.CurrentWorkbook(){[Name="TBL_NBA"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"TEAM", type text}, {"WINS", Int64.Type}, {"LOSSES", Int64.Type}, {"NET_RATING", type number}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"WINS", Order.Descending}, {"NET_RATING", Order.Descending}, {"TEAM", Order.Ascending}})
    in
    #"Sorted Rows"

    What interests us is the final part with Table.Sort. As we mentioned, we’re sorting a table according to a predefined scheme, where we first have the table obtained from “Changed Type”, that is, the previous step. At this point, we’re working with pairs of lists, where we first specify the column and then the sort order. {"WINS", Order.Descending} means we take the “Wins” column and sort it from highest to lowest. Since there are multiple conditions, the second part of Table.Sort becomes a list of lists, where we have multiple lists arranged in a hierarchical order: Wins #1, Rating #2, Team #3. What we see on the screen is therefore an exact reflection of what we see in the code.

    Conclusion

    There are many different ways to sort data in Excel, including standard features, functions, and Power Query. Depending on the situation, you might want to use all three options, though I prefer working with functions or Power Query, since I find sorting directly from the table to be somewhat “unreliable,” especially when multiple people are working on the same file. Once you get used to working with Excel and tables, you won’t look at the source data anymore, so sorting it becomes almost irrelevant.

    If there are specific scenarios you’d like to see covered in this guide, let me know in the comments!

    Leave a Reply

    Your email address will not be published. Required fields are marked *