Order function in excel

Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel for iPad Excel for iPhone Excel for Android tablets Excel for Android phones More…Less

The SORT function sorts the contents of a range or array. 

In this example, we’re sorting by Region, Sales Rep, and Product individually with =SORT(A2:A17), copied across cells F2, H2, and J2.

Use the SORT function to sort ranges of data. Here we're using =SORT(A2:A17) to sort Region, then copied to cells H2 & J2 to sort Sales Rep name, and Product.

SORT returns a sorted array of the elements in an array. The returned array is the same shape as the provided array argument. 

=SORT(array,[sort_index],[sort_order],[by_col])

Argument

Description

array

Required

The range, or array to sort

[sort_index]

Optional 

A number indicating the row or column to sort by

[sort_order]

Optional

A number indicating the desired sort order; 1 for ascending order (default), -1 for descending order

[by_col]

Optional

A logical value indicating the desired sort direction; FALSE to sort by row (default), TRUE to sort by column

Notes: 

  • Where sort_index is not provided, row1/col1 will be presumed. Where order is not provided, ascending order will be presumed. By default Excel will sort by row, and will only sort by column where by_col is TRUE. When by_col is FALSE or missing Excel will sort by row.

  • The SORT function is provided to sort data in an array. If you want to sort data in the grid, it’s better to use the SORTBY function, as it is more flexible. SORTBY will respect column additions/deletions, because it references a range, where SORT references a column index number.

  • An array can be thought of as a row of values, a column of values, or a combination of rows and columns of values. In the example above, the source array for our SORT formula is range A5:D20.

  • The SORT function will return an array, which will spill if it’s the final result of a formula. This means that Excel will dynamically create the appropriate sized array range when you press ENTER. If your supporting data is in an Excel Table, then the array will automatically resize as you add or remove data from your array range if you’re using Structured References. For more details see this article on Spilled Array Behavior.

  • Excel has limited support for dynamic arrays between workbooks, and this scenario is only supported when both workbooks are open. If you close the source workbook, any linked dynamic array formulas will return a #REF! error when they are refreshed.

Examples

Sort a range of values in descending order.

Sort a range of values in descending order.

Use SORT and FILTER together to sort a range in ascending order, and limit it to values over 5,000.

Use SORT and FILTER together to sort a range in ascending order, and limit it to values over 5,000.

Need more help?

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

See Also

FILTER function

RANDARRAY function

SEQUENCE function

SORTBY function

UNIQUE function

#SPILL! errors in Excel

Dynamic arrays and spilled array behavior

Implicit intersection operator: @

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

The SORT function sorts the contents of a range or array in ascending or descending order with a formula. The result from SORT is a dynamic array of values that will «spill» onto the worksheet into a range. If values in the source data change, the result from SORT will update automatically. 

The SORT function takes four arguments: arraysort_index, sort_order, and by_col. The first argument, array, is the range or array to be sorted. This is the only required argument. By default, the SORT function will sort values in ascending order using the first column in array. Use the optional arguments sort_index and sort_order to control which column to sort by, and the order to sort by (ascending or descending). Sort_index should be a number corresponding to the column (or row) used for sorting. For example, to sort by the third column in a range of data, use 3 for sort_index. The optional sort_order argument determines sort direction. Use 1 for ascending order and -1 for descending order. By default, the SORT function will sort data vertically by rows. To sort a range horizontally by columns, set the fourth argument, by_col, to TRUE.

Basic Examples

To sort a range by the first column in ascending order:

=SORT(range) // sort by column 1, ascending
=SORT(range,1,1) // sort by column 1, ascending

To sort a range by the first column in descending order:

=SORT(range,1,-1) // sort by column 1, descending

To sort a range by the second column in descending order:

=SORT(range,2,-1) // sort by column 2, descending

To sort a range horizontally (by column) using values in row 1 in descending order, set the fourth argument, by_col, to TRUE or 1:

=SORT(range,1,-1,TRUE)
=SORT(range,1,-1,1)

Example — sort by score

SORT function example - sort by score, descending

In the example shown above, data includes names in column B and scores in column C. In cell E5, the SORT function is used to sort the data by score in descending order:

=SORT(B5:C14,2,-1) // sort by scores in descending order

The SORT function extracts all values, sorted in descending order by score, and results «spill» into the range E5:F14. To sort by score in ascending order, omit sort_order or to 1 like this:

=SORT(B5:C14,2) // sort by score in ascending order
=SORT(B5:C14,2,1) // sort by score in ascending order

Sort by more than one level

Unlike the SORTBY function, the SORT function does not provide a way to sort data by more than one level. However, by using array constants for sort_index and sort_order, it can be done. This video explains how.

Notes

  • SORT returns a #VALUE error if sort_index is out of range.

Sorting the data in Excel is a tool for presenting information in a user-friendly form. Numerical values can be sorted in ascending and descending order, text values can be sorted alphabetically and in reverse order. Available options are the sorting by color and font, in random order, according to several conditions. Columns and rows are sorted.



Sorting order in Excel

There are two ways to open the sort menu:

  1. Right-click on the table. Select «Sort» and method «Largest to Smallest».
  2. .

  3. Open the «DATA» tab — «Sort» dialog box.

.

Frequently used sorting methods are represented by a single button on the «HOME» tab «Editing» section.

.

Sorting the table by a separate column:

  1. For the program to perform the task correctly, select the required column in the data range.
  2. .

  3. Then we act in accordance with the task. If you want to perform simple sorting in ascending / descending order (alphabet or vice versa), just click the corresponding button on the taskbar. When the range contains more than one column, Excel opens a dialog box like:
    .
    To maintain the correspondence of the values in the lines, we select the action «Expand the selection». Otherwise, only the selected column is sorted — the structure of the table is broken.

.

If you select the entire table and sort it, the first column is sorted. Data in the rows will be in accordance with the position of the values in the first column.



Sorting by the cell color and font

Excel provides rich formatting capabilities to the user. Therefore, it is possible to operate in different formats.

We will make the column «Total» and «fill» the cells with values in different shades in the training table. Perform the color sorting:

  1. Select the column — the right mouse button — «Sort».
  2. .

  3. From the proposed list, select «Put Selected Cell Color On Top».
  4. .

  5. We agree to «Expand the selection». Result:

.

The program sorted cells by accents. The user can choose the order of the color sorting. For this purpose, in the list of tool options, select «Custom Sort».

.

In the window that opens, enter the required parameters:

.

Here you can choose the order of the representation the cells of different colors.

.

By the same principle, the font data is sorted.

Sorting by multiple columns in Excel

How to set the order of secondary sorting in Excel? To solve this problem, you need to set several sorting conditions.

  1. Open the menu «Custom Sort». Assign the first criterion.
  2. .

  3. Click the button «Add Level».
  4. .

  5. There are windows for entering data for the next sorting condition. We fill them.

.

The program allows you to add several criteria at once to perform the sorting in a special order.

Sorting the rows in Excel

By default, the data is sorted by columns. How to sort by rows in Excel:

.

  1. In the «Custom Sort» dialog box, click the «Options» button.
  2. .

  3. In the menu that opens, select «Range Columns».
  4. .

  5. Click OK. In the «Sort» window, fields for filling in the conditions for the rows appear.

.

Thus, the table is sorted into Excel by several parameters.

Random sorting in Excel

The built-in sorting options do not allow you to arrange data in a column randomly. This task will be handled by the function =RAND().

For example, you need to arrange a set of certain numbers in a random order.

.

We put the cursor in the next cell (left-right, not important). In the formula row, enter =RAND(). Press Enter. We copy the formula to the whole column — we get a set of random numbers.

.

Now sort the resulting column in ascending / descending order — the values in the original range will automatically be placed in random order.

Dynamic table sorting in Excel

If you apply standard sorting to the table, then when you change the data, it will not be relevant. You need to make sure that the values are sorted automatically. We use formulas.

  1. There is a set of primes that need to be sorted in ascending order.
  2. .

  3. Put the cursor in the next cell and enter the formula: =SMALL(A:A,ROW(A1)). Exactly, as a range we specify the whole column. And as a coefficient there is the function of SMALL() ROW() with reference to the first cell.
  4. .

  5. Lets change the number in the initial range to 7 to 25 — «sorting» ascending will also change.

.

If it is necessary to make the dynamic sorting in descending order, use the =LARGE() function.

To sort the text values dynamically, you need the array formulas.

  1. Initial data is a list of some names in any order. In our example it is a list of fruits.
  2. .

  3. Select the column and give it the name «fruits». To do this, in the field of names that is near the formula line, enter the name we need to assign it to the selected range of cells.
  4. .

  5. In the next cell (in the example — in B1), we write the formula:

    Since we have an array formula before us, press Ctrl + Shift + Enter. We multiply the formula by the whole column.

  6. .

  7. If lines are added to the original column, then we introduce a slightly modified formula:

Add the «pomelo» value to the «fruit» name range and check:

Download sorting data in excel using formulas

.

Subsequently, when you add data to the table, the sorting process will be performed automatically.

The SORT function is introduced in Excel 365 online. It is an dynamic array function that sort the given array into ascending and descending orders. This function is not available in Excel 2016 and older versions.
In this article, we will learn how to use the SORT function to sort arrays and tables in excel.
Syntax of SORT Function

=SORT(array,[sort_index],[sort_order],[by_col])

Array : It is the reference of array or range that you want to sort.

[sort_index] : The column number in the two dimensional array by which you want to sort the range. By Default it is 1.

[sort_order] : The order by which you want to sort the array. For ascending it is 1 and for descending it is -1. By default it is 1.

[by_col] : Set it True(1) if you want to sort an horizontal array. By default it False(0) for Vertical data.

Let’s understand this SORT function with some examples
Example 1: Sort Entire Table in Ascending Order By First Column.
Here I have this sample data table that I want to sort with the first column in ascending order.

The formula is simple:

=SORT(A2:C10)

It will return a dynamic array and will fill the sheet with the resulting sorted array.

Example 2: Sort Table By Column 2 in Descending Order
In our table, the second column is the Roll No. We need to sort this table by roll no. in reverse order.
The formula for sorting array in descending order of column 2 will be:

=SORT(A2:C10,2,-1)


Example 3: Sort the Horizontal Data in Ascending Order

Here, I have data of different years. I want to sort this data by collection in ascending order. The SORT formula for this will be:

=SORT(C2:I3,2,,1)

Here, we wanted to sort horizontally, so we set by_col variable to 1(TRUE). Since we wanted sort by the second row, the sort index is set to 2. We left the order_by to be the default.

How it works

The formula works automatically in the background to sort the given arrays.

Note: This function is only available in office 365 online version. It is not available in Excel 2016 or older versions. To sort arrays in Excel 2016 and older versions use these sorting formulas.

So yeah guys, this was a quick tutorial of how to use SORT function to sort arrays in Excel. I hope I was explanatory enough. If you have any doubts regarding this query or any other EXCEL VBA related query, ask them in the comments section below.

Related Articles:

Sort numbers using Excel SMALL Function | To sort numbers using formula in Excel 2016 and older, you can use the SMALL function with other helping functions.

Sort Numeric Values with Excel RANK Function | To sort the numeric values we can use the Rank function. The formula is 

Excel Formula to Sort Text | To sort text values using formula in excel we simply use the COUNTIF function. Here is the formula

Popular Articles:

50 Excel Shortcuts to Increase Your Productivity | Get faster at your task. These 50 shortcuts will make you work even faster on Excel.

The VLOOKUP Function in Excel | This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets. 

COUNTIF in Excel 2016 | Count values with conditions using this amazing function. You don’t need to filter your data to count specific values. Countif function is essential to prepare your dashboard.

How to Use SUMIF Function in Excel | This is another dashboard essential function. This helps you sum up values on specific conditions.

The SORT function in Excel, is one of Excel’s best new features. It’s one of a group of functions that make use of Excel’s new dynamic array calculation engine, enabling Excel to spill results into multiple cells from a single formula.

At the time of writing, the SORT function is only available for Microsoft 365, Excel 2021 and Excel Online. It will not be available in Excel 2019 or earlier versions.

Download the example file: Click the link below to download the example file used for this post:

Watch the video:

Watch the video on YouTube

Arguments of the SORT function in Excel

Before we look at the arguments required for the SORT function, let’s look at a basic example to appreciate what it does.

As demonstrated below, the SORT function does exactly what you would expect; it sorts the data. The critical point is to notice that it achieves this with a single function in a single cell and spills the other results into the cells below.

Basic SORT Functionality

SORT has four arguments:

=SORT(array, [sort_index], [sort_order], [by_col])
  • array: The range of cells, or array of values to be sorted.
  • [sort_index]: The nth column or row to apply the sort to. For example, to sort by the 2nd column, the sort index would be 2. It is possible to sort by multiple columns (covered in Example 6 below). If this argument is excluded, Excel defaults to sorting by the first column.
  • [sort_order]: contains either:
    1 = sort in ascending order
    -1 = sort in descending order
    If excluded the argument defaults to 1.
  • [by_col]: can be either:
    • TRUE = sort by columns
    • FALSE = sort by rows
    If excluded the argument defaults to FALSE.

The following examples illustrate how to use the SORT function.

Example 1 – SORT returns an array of rows and columns

In this example, a single formula sorts the values in the first column and returns the full range of cells provided by the array argument.

SORT Spill Range with default arguments

The formula in cell G3 is:

=SORT(B3:E10)

This single formula is returning eight rows and four columns of data. As the second, third, and fourth arguments have been excluded, the defaults are applied for each of them, sorting by the first column, in ascending order with data organized in rows.

Example 2 – SORT by another column in descending order

Example 2 shows how to sort by the second column in descending order.

SORT on column 2 in reverse order

The formula in cell G3 is:

=SORT(B3:E10,2,-1)

The second argument in the SORT function is the sort_index. Therefore, by using 2 as the sort_index, the formula above is sorted by the 2nd column of the array.

The third argument is the sort_order. The -1 in this argument denotes the data sorts in descending order.

Example 3 – SORT expands automatically when linked to a Table

The animation below shows how the SORT function responds when linked to an Excel table.

SORT with tables

In this example, the SORT function is using an Excel Table as its source data array. New records added to the Table are automatically added to the spill range of the function.

Example 4 – Using SORT to return a top 5 and specific columns

Example 4 shows how to create a top 5 and select which specific columns to return. As this example also uses the INDEX and SEQUENCE functions, it will work in Excel 2021 and Excel 365.

SORT to return top 5 in order

The formula in cell G3 is:

=INDEX(SORT(B3:E10,4,-1),SEQUENCE(5),{1,4})

We are using two additional functions in this example, SEQUENCE (also a new function) and INDEX (which is not new, it has been around forever).

SORT is applied to the 4th column in descending order, we have seen similar examples to this above.

INDEX takes the result of the SORT function and uses:

  • The SEQUENCE function to only show the first 5 results
  • A constant array to display only columns 1 and 4.

In the past, this would have needed a lot of calculations, but now it’s possible with a single function – Amazing!

If using Excel 365, an alternative function that uses the CHOOSEROWS and CHOOSECOLS can be found below:

=CHOOSECOLS(CHOOSEROWS(SORT(B3:E10,4,-1),SEQUENCE(5)),1,4)
=CHOOSECOLS(CHOOSEROWS(SORT(B3:E10,4,-1),SEQUENCE(5)),{1,4})

In both of these, SEQUENCE creates an array {1,2,3,4,5}, which is used inside the CHOOSEROWS function to return only the first 5 rows. CHOOSECOLS then returns only columns 1 and 4.

Example 5 – Combining FILTER and SORT

The dynamic array functions can be nested within each other. This example shows the FILTER function nested inside SORT.

SORT with FILTER

The formula in cell G3 is:

=SORT(FILTER(B3:E10,C3:C10>=100))

The FILTER function returns only three rows, where the values in cells C3-C10 are 100 or higher. SORT is then applied to the result of the FILTER, to provide those filtered rows in alphabetical order.

Example 6 – SORT on multiple columns

SORT can be applied to multiple columns at the same time.

SORT with multiple criteria

The formula in cell G3 is:

=SORT(B3:E10,{2,1},{1,-1})

This formula contains two constant arrays. The first, {2,1} is the sort_order, which in this example is sorting by column 2 then by column 1. The second constant array is {1,-1}, which determines how each column sorts. The first sort (applied to column 2) is in ascending order, and the second sort (applied to column 1) is in descending order.

Want to learn more?

There is a lot to learn about dynamic arrays and the new functions. Check out my other posts here to learn more:

  • Introduction to dynamic arrays – learn how the excel calculation engine has changed.
  • UNIQUE – to list the unique values in a range
  • SORT – to sort the values in a range
  • SORTBY – to sort values based on the order of other values
  • FILTER – to return only the values which meet specific criteria
  • SEQUENCE – to return a sequence of numbers
  • RANDARRAY – to return an array of random numbers
  • Using dynamic arrays with other Excel features – learn to use dynamic arrays with charts, PivotTables, pictures etc.
  • Advanced dynamic array formula techniques – learn the advanced techniques for managing dynamic arrays

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn’t until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).


Do you need help adapting this post to your needs?

I’m guessing the examples in this post don’t exactly match your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Sorting data in Excel has been made quite easy with all the in-built options.

You can easily sort your data alphabetically, based on the value in the cells, or by cell and font color.

You can also do multi-level column sorting (i.e., sorting by column A and then by column B) as well as sorting rows (from left to right).

And if that was not enough, Excel also allows you to create your own custom lists and sort based on that too (how cool is that). So you can sort data based on shirt sizes (XL, L, M, S) or responses (strongly agree, agree, disagree) or intensity (high, medium, low)

Bottom line – there are just too many sorting options you have at your disposal when working with Excel.

And in this massive in-depth tutorial, I will show you all these sorting options and some cool examples where these can be useful.

Since this a huge tutorial with many topics, I am providing a table of content below. You can click on any of the topics and it will instantly take you there.

Accessing the Sorting Options in Excel

Since sorting is such a common thing needed when working with data, Excel provides a number of ways for you to access the sorting options.

Sorting Buttons in the Ribbon

The fastest way to access sorting options is by using the sorting buttons in the ribbons.

When you click on the Data tab in the ribbon, you will see the ‘Sort & Filter’ options. The three buttons on the left in this group is for sorting the data.

Sort Option in the Data tab in Excel

The two small buttons allow you to sort your data as soon as you click on these.

Sort icons in the Data tab in Ribbon in Excel

For example, if you have a data set of names, you can just select the entire dataset and click on any of the two buttons to sort the data. The A to Z button sorts the data alphabetically lowest to highest and the Z to A button sorts the data alphabetically highest to lowest.

This buttons also work with numbers, dates or times.

Personally, I never use these buttons as these are limited in what these can do, and there are more chances of errors when using these. But in case you need a quick sort (where there are no blank cells in the dataset), these can be a fast way to do it.

Sorting Dialog Box

In the Data tab in the ribbon, there is another Sort button icon within the sorting group.

Sort Button to open the dialog box

When you click this Sort button icon, it opens the sorting dialog box (something as shown below).

Sort in Excel - Dialog box

Sorting dialog is the most complete solution for sorting in Excel. All the sorting related options can be accessed through this dialog box.

All the other methods of using sorting options are limited and don’t offer full functionality.

This is the reason I always prefer using the dialog box when I have to sort in Excel.

A major reason for my preference is that there is very less chance of you going wrong when using the dialog box. Everything is well structured and marked (unlike buttons in the ribbon where you may get confused which one to use).

In this tutorial, you’ll find me mostly using the dialog box to sort the data. This is also because some of the things I cover in certain sections (such as multi-level sorting or sorting from left-to-right) can only be done using the dialog box.

Keyboard Shortcut – If you need to sort data often in Excel, I recommend you learn the keyboard shortcut to open the sort dialog box. It’s ALT + A + S + S

Sorting Options in the Filter Menu

If you have applied filters to your dataset, you can also find the sorting options along with the filter options. A filter can be applied by selecting any cell in the dataset, clicking on the Data tab and clicking in the Filter icon.

Filter Icon in Excel Ribbon

Suppose you have a dataset as shown below and you have the filter applied.

Filter applied to a dataset

When you click on the filter icon for any column (it’s the small downward pointing triangle icon at the right of the column header cell), you’ll see some of the sorting options there as well.

Sort in Excel using Filter drop down options

Note that these sorting options change based on the data in the column. So if you have text, it will show you the option to sort from A to Z or Z to A, but if you have numbers, it will show you options to sort from largest to smallest or smallest to largest.

Right-click options

Apart from using the dialog box, using right-click is another method that I sometimes use (it’s also super fast as it only takes two clicks).

When you have a dataset that you want to sort, right-click on any cell and it will show you the sorting options.

Sort in Excel using right-click menu options

Note that you see some options that you don’t see in the ribbon or in the Filter options. While there is the usual sort by value and custom sort option (which opens the Sort dialog box), you can also see options such as Put selected Cell color/Font color/Formatting icon on the top.

I find this option quite useful as it allows me to quickly get all the colored cells (or cells with a different font color) together at the top. I often have the monthly expense data that I go through and highlight some cells manually. I can then use this option to quickly get all these cells together at the top.

Now that I have covered all the ways to access sorting options in Excel, let’s see how to use these to sort data in different scenarios.

Also read: How to Undo Sort in Excel (Revert to Original)

Sorting Data in Excel (Text, Numbers, Dates)

Caveat: Most of the times, sorting works even when you select a single cell in the dataset. But in some cases, you may end up with issues (when you have blank cells/rows/columns in your dataset). When sorting data, it’s best to select the entire data set and then sort it – just to avoid any chance of issues.

Based on the type of data you have, you can use the sorting options in Excel.

Sorting by Text

Suppose you have a dataset as shown below and you want to sort all these records based on the name of the student in alphabetical order.

Student Name dataset for sorting

Below are the steps to sort this text data in alphabetical order:

  1. Select the entire dataset
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.Sort Button to open the dialog box
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.Make Sure My Data has headers option is selected
  5. In the ‘Sort by’ drop-down, select ‘Name’Select Name in Sort by drop down
  6. In the ‘Sort On’ drop-down, make sure ‘Cell Values’ is selectedSelect Cell Values in Sort on drop down
  7. In the Order drop-down, select A-ZSelect A to Z in Sort Order dialog box
  8. Click OK.

The above steps would sort the entire dataset and give the result as shown below.

Student name data sorted alphabetically

Why not just use the buttons in the ribbon?

The above method of sorting data in Excel may look like a lot of steps, as compared to just clicking the sort icon in the ribbon.

And this is true.

The above method is longer, but there is no chance of any error.

When you use the sort buttons in the ribbon, there are a few things that can go wrong (and this could be hard to spot when you have a large data set.

While I discuss the drawbacks of using the buttons later in this tutorial, let me quickly show you what can go wrong.

In the below example, since Excel cannot recognize that there is a header row, it sorts the entire dataset, including the header.

Sort Icon Button Issue - Column Header not recognized

This issue is avoided with the sort dialog box as it explicitly gives you the option to specify whether your data has headers or not.

Since using the Sort dialog box eliminates chances of errors, I recommend you use it instead of all the other sorting methods in Excel

Sorting by Numbers

Now, I am assuming you have already gotten a hang of how text sorting works (covered above this section).

The other types of sorting (such as based on numbers or dates or color) is going to use almost the same steps with minor variations.

Suppose you have a dataset as shown below and you want to sort this data based on the marks scored by each student.

Sort by Student Score by Numbers - Dataset

Below are the steps to sort this data based on numbers:

  1. Select the entire dataset
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.
  5. In the ‘Sort by’ drop-down, select ‘Name’
  6. In the ‘Sort On’ drop-down, make sure ‘Cell Values’ is selected
  7. In the Order drop-down, select ‘Largest to Smallest’
  8. Click OK.

The above steps would sort the entire dataset and give the result as shown below.

Student Score sorted from largest to smallest

Sorting by Date/Time

While the date and time may look different, they are nothing but numbers.

For example, in Excel, the number 44196 would be the date value for 31 December 2020. You can format this number to look like a date, but in the backend in Excel, it still remains a number.

This also allows you to treat dates as numbers. So you can add 10 to a cell with date, and it will give you the number for the date 10 days later.

And the same goes for the time in Excel.

For example, the number 44196.125 represents 3 AM on 31st December 2020. While the integer part of the number represents a full day, the decimal part would give you the time.

And since both date and time are numbers, you can sort these like numbers.

Suppose you have a dataset as shown below and you want to sort this data based on the project submission date.

Dataset to Sort by Dates in Excel

Below are the steps to sort this data based on the dates:

  1. Select the entire dataset
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.
  5. In the ‘Sort by’ drop-down, select ‘Submission Date’
  6. In the ‘Sort On’ drop-down, make sure ‘Cell Values’ is selected
  7. In the Order drop-down, select ‘Oldest to Newest’
  8. Click OK.

The above steps would sort the entire dataset and give the result as shown below.

Data sorted by dates - Result

Note that while date and time are numbers, Excel still recognized that these are different in the way it’s displayed. So when you sort by date, it shows ‘Oldest to Newest’ and ‘Newest to Oldest’ sorting criteria, but when you use numbers, it shows ‘Largest to Smallest’ or ‘Smallest to Largest’. Such small things that makes Excel an awesome spreadsheet tool (PS: Google Sheets doesn’t show so much detail, just the bland sort by A-Z or Z-A)

Sorting by Cell Color / Font Color

This option is amazing and I use it all the time (maybe a tad bit too much).

I often have data sets that I analyze manually and highlight cells while I am doing it. For example, I was going through a list of articles on this blog (which I have in an Excel sheet) and I highlighted the ones which I needed to improve.

And once I am done, I can quickly sort this data based on the cell colors. This helps me in getting all these highlighted cells/rows together at the top.

And to add to the awesomeness, you can sort based on multiple colors. So if I highlight cells with article names which need immediate attention in red and some which can be dealt with later with yellow, I can sort the data to show all the red rows first followed by the yellow.

If you’re interested in learning more, I recently wrote this article on sorting based on multiple colors. In this section, I will quickly show you how to sort based on one color only

Suppose you have a dataset as shown below and you want to sort by color and get all the red cells at the top.

Sort Data by Color in Excel - Dataset

Below are the steps to sort by color:

  1. Select the entire dataset
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.
  5. In the ‘Sort by’ drop-down, select ‘Submission Date’ (or whatever column where you have the colored cells).Since in this example we have colored cells in all the columns, you can choose any.
  6. In the ‘Sort On’ drop-down, select ‘Cell Color’.
  7. In the Order drop-down, select the color with which you want to sort. In case you have multiple cell colors in the dataset, it will show you all those colors hereSelect color by which you want to sort the data
  8. In the last drop-down, select ‘On Top’. This is where you specify whether you want the colored cells to be on the top of the dataset or at the bottom.
  9. Click OK.

The above steps would sort your dataset by color and you will get the result as shown below.

Sort Data by color - result

Just as we have sorted this data based on the cell color, you can also do it based on the font color and conditional formatting icon.

Multiple Level Data Sorting

In reality, datasets are rarely as simple as the one that I am using in this tutorial.

Yours may extend to thousands of rows and hundreds of columns.

And when you have datasets so big, there is also a need for more data slice and dice. Multiple level data sorting is one of the things you may need when you have a large data set.

Multiple level data sorting means that you can sort the dataset based on values in one column and then sort it again based on values in another column(s).

For example, suppose you have the dataset as shown below and you want to sort this data based on two criteria:

  1. The region
  2. The sales

Multi-level sorting in Excel - Dataset

The output of this sorting based on the above two criteria will give you a dataset as shown below.

In the above example, we have the data first sorted by the regions and then within each region, the data is further sorted by the sales value.

This quickly allows us to see which sales reps are doing great in each region or which ones are doing poorly.

Below are the steps to sort the data based on multiple columns:

  1. Select the entire data set that you want to sort.
  2. Click the Data tab.
  3. Click on the Sort Icon (the one shown below). This will open the Sort dialog box.
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.
  5. In the Sort Dialogue box, make the following selections
    • Sort by (Column): Region (this is the first level of sorting)
    • Sort On: Cell Values
    • Order: A to Z
  6. Click on Add Level (this will add another level of sorting options).Click on Add Level to add another level of sorting for the dataset
  7. In the second level of sorting, make the following selections:
    • Then by (Column): Sales
    • Sort On: Values
    • Order: Largest to SmallestSecond Level sorting criteria in dialog box
  8. Click OK

Pro Tip: The Sort dialog box has a feature to ‘Copy Level’. This quickly copies the selected sorting level and then you can easily modify it. It’s good to know feature and may end up saving you time in case you have to sort based on multiple columns.

Below is a video where I show how to do a multi-level sorting in Excel:

Sorting Based on a Custom List

While Excel already has some common sorting criteria (such as sorting alphabetically with text, smallest to largest or largest to smallest with numbers, oldest to newest or newest to oldest with dates), it may not be enough.

To give you an example, suppose I have the following dataset:

Dataset to sort based on region

Now, if I sort it based on the region alphabetically, I have two options – A to Z or Z to A. Below is what I get when I sort this data alphabetically from A to Z using the region column.

Sorting Region Data Alphabetically A to Z

But what if I want this sort order to be East, West, North, South?

You, of course, can rearrange the data after sorting, but that’s not the efficient way to do this.

The right way to do this would be using custom lists.

A custom list is a list that Excel allows you to create and then use just like the inbuilt lists (such as month names or weekday names). Once you have created a custom list, you can use it in features such as data sorting or fill handle.

Some example where custom lists can be useful include:

  • Sorting the data based on region/city name
  • Sorting based on T-shirt sizes – Small, Medium, Large, Extra Large
  • Sorting based on survey responses – Strongly Agree, Agree, Neutral, Disagree
  • Sorting based on probability – high, medium, low

The first step in trying to sort based on the custom criteria is to create the custom list.

Steps to create a custom list in Excel:

  1. Click the File tabClick the File Tab
  2. Click on OptionsClick on Options
  3. In the Excel Options Dialogue Box, select ‘Advanced’ from the list in the left pane.Click on Advanced in the left pane
  4. Within Advanced selected, scroll down and select ‘Edit Custom List’.Click on Edit Custom List
  5. In the Custom Lists dialogue box, type your criteria in the box titled List Entries. Type your criteria separated by comma (East, West, North, South) [you can also import your criteria if you have it listed].Specify the custom sort order criteria
  6. Click Add
  7. Click OK

Once you have completed the above steps, Excel will create and store a custom list that you can use for sorting data.

Note that the order of the items in the custom list is what determines how your list will be sorted.

When you create a custom list in one Excel workbook, it becomes available to all the workbooks in your system. So you only have to create it once and can reuse it in al workbooks.

Steps to sort using a custom list

Suppose you have the dataset as shown below and you want to sort it based on the regions (the sort order being East, West, North, and South)

Since we have already created a custom list, we can use it to sort our data.

Here are the steps to sort a dataset using a custom list:

  1. Select the entire dataset
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.
  5. In the ‘Sort by’ drop-down, select ‘Region’ (or whatever column where you have the colored cells)
  6. In the ‘Sort On’ drop-down, select ‘Cell Values’.
  7. In the Order drop-down, select ‘Custom List’. As soon as you click on it, it will open the Custom Lists dialog box.Click on Custom List and the Sort Order
  8. In the Custom Lists dialog box, select the custom list you have already created from the left pane.Select the custom sort criteria
  9. Click OK. Once you do this, you will see the custom sort criteria in the sort order drop-down fieldCustom Sort criteria becomes available as the sort order
  10. Click OK.

The above steps will sort your dataset based on the custom sort criteria.

Sorting region based on custom criteria - resulting data

Note: You don’t have to create a custom list before-hand to sort the data based on it. You can also create it while you’re in the Sort dialog box. When you click on Custom List (in step 7 above), it opens the custom list dialog box. You can create a custom list there as well.

Custom lists are not case sensitive. In case you want to do a case-sensitive sorting, refer to this example.

Also read: How to Sort by Length in Excel? Easy Formulas!

Sorting from Left to Right

While in most cases you’ll likely sort based on the column values, sometimes, you may also have a need to sort based on the row values.

For example, in the below dataset, I want to sort it based on the values in the Region row.

Sort data left to right in Excel - Dataset

Although this type of data structure is not as common as the columnar data, I have still seen a lot of people working with this kind of construct.

Excel has an in-built functionality that allows you to sort from left to right.

Below are the steps to sort this data from left to right:

  1. Select the entire dataset (except the headers)
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.
  4. In the Sort dialog box, click on Options.Click on Option to sort left to right in Excel
  5. In the Sort Options dialog box, select ‘Sort left to right’Select Sort Left to Right
  6. Click OK.
  7. In the ‘Sort By’ drop-down, select Row 1. By doing this, we are specifying that the sort needs to be done based on the values in Row 1
  8. In the ‘Sort On’ drop-down, select ‘Cell Values’.
  9. In the Order drop-down, select A-Z (you can also use custom sort list if you want)
  10. Click OK.

The above steps would sort the data left to right based on Row 1 values.

Left to Right Sort Result

Excel doesn’t recognize (or even allow you to specify) the headers when sorting from left to right. So you need to make sure your header cells are not selected when sorting the data. If you select headers cells as well, these will be sorted based on the value in it.

Note: Another way of sorting the data from right to left could be to transpose the data and get it in a columnar form. Once you have it, you can use any of the sorting methods covered so far. Once the sorting is done, you can then copy the resulting data and paste it as transposed data.

Below is a video where I show how to sort data from left to right in Excel

Case Sensitive Sorting in Excel

So far, in all the example above, the sorting has been case independent.

But what if you want to make the sorting case-sensitive.

Thankfully, Excel allows you to specify whether you want the sorting to be case-sensitive or not.

Note: While most of the times you don’t need to worry about case-sensitive sorting, this may be helpful when you get your data from a database such as Salesforce or you manually collate the data and have different people enter the same text in different cases. Having a case sensitive sort may help you keep all the records by the same person/database together.

Suppose you have a dataset as shown below and you want to sort this data based on the region column:

Case Sensitive sort in Excel - dataset

Below are the steps to sort data alphabetically as well as make it case sensitive:

  1. Select the entire dataset
  2. Click the Data tab
  3. Click the Sort icon. This will open the Sort dialog box.
  4. In the Sort dialog box, make sure my data has headers is selected. In case your data doesn’t have headers, you can uncheck this.
  5. Click the Options buttonClick the Option button in the Sort dialog box
  6. In the Sort Options dialog box, check the ‘Case sensitive’ optionCheck the case sensitive option
  7. Click OK.
  8. In the ‘Sort by’ drop-down, select ‘Region’
  9. In the ‘Sort On’ drop-down, select ‘Cell Values’.
  10. In the order drop-down, select A to Z
  11. Click OK.

The above steps would not only sort the data alphabetically based on the region, but also make it case-sensitive.

You’ll get the resulting data as shown below:

Case Sensitive Sort in Excel - Result

When you sort from A to Z, lower case text is placed above the upper case text.

Getting the Original Sort Order

Often when sorting data in Excel, you may want to go back to the earlier or the original sort order and start afresh,

While you can use the undo functionality in Excel (using Control Z) to go back one step at a time, it may be confusing if you have already done multiple things after sorting the data.

Also, undo works only till you have the workbook open, but when you save and close and it and open it later, you won’t be able to go back to the original sort order.

Here are two simple ways to make sure you don’t lose the original sort order and get it back even after sorting the data:

  1. Make a copy of the original dataset. This is something I recommend you do even if you don’t need the original sort order. You can have a workbook with all the data and then simply create a copy of the workbook and work on the copy. When I am working on critical datasets, I make a copy every day (with the date or version number as a part of the workbook name).
  2. Add a column with a series of number. This series of numbers get all mixed up when you sort the data, but in case you want to go back to the original data, you can sort based in this series of numbers.

In this section, let me quickly show you what mean by adding a series and using it to get original sort order back.

Suppose you have a dataset as shown below:

Data set for sorting - without helper column

To make sure you have a way to get back this data after sorting it, add a helper column and have a series of numbers in it (as shown below).

Dataset with helper column

Here is a tutorial with different ways to add a column with numbers quickly

When you have the helper column in place, make sure you include it while sorting this dataset.

Suppose I sort this data based on the region and end up getting the data as shown below:

Helper column also get sorted when you sort the data

Now, if I want to go back to the original dataset, I can simply sort this data again, but based on the helper column (from low to high).

Simple.. isn’t it?

In case you don’t want the helper column to show, you can either hide it or create a backup and then delete it.

Some Common Issues While Sorting Data in Excel

At the beginning of this article, I showed you different ways to sort data in Excel (including sort buttons in the ribbon, right-click options, filter option, and the sort dialog box).

And to reiterate, using the sort dialog box minimizes the chances or any issues or error that may arise.

Now, let me show you what can go wrong when you use the sort button from the ribbon (the ones shown below)

Not Identifying the Column Headers

Suppose you have a dataset as shown below:

This looks like a decently formatted data set with headers clearly formatted with cell color and bold font.

So when you sort this data based on the names (using the sort buttons in the ribbon), you would expect the header to remain at the top and rest of the data to get sorted.

But what happens when you do it – the header is also considered as normal data and gets sorted (as shown below).

Sort Icon Button Issue - Column Header not recognized

While Excel is smart enough to recognize headers, in this example, it failed to do so. When using the sort icon buttons in the ribbon, there was no way for you to manually specify that this data has headers.

Note: This issue occurs when I have the dataset and I add and format the headers and sort it. Normally, Excel would be smart enough to identify that there are headers in a dataset (especially when the data type of the header and the data in the column is different). But in this case, it failed to do so when I added the header and sorted right away. If I save this workbook, close it and reopen it, Excel somehow manages to identify the first row and header.

While this may not be an issue in most cases, there is still a chance when you use the sort icons in the ribbon, Using a dialog box eliminates this issue as you can specify that you have headers.

Not Identifying Blank Rows/Columns

This sorting issue is a little more complex – and a lot more common than you would imagine.

Suppose you have a dataset as shown below. Note that row number 6 is hidden (and it’s a blank row).

Row is blank and is hidden in the dataset

Now, if I select any cell in the first four rows of the data (the ones above the hidden blank row) and sort this data using the sort icon buttons, it would only sort the first four records on the data set (row 2 to 5). Similarly, if I select any cell in the dataset below the hidden blank row, it will only sort the last seven rows.

Issue with blank hidden row when sorting

And unless you’re looking for it specifically, you’re likely to miss this horrible error.

How to make sure you don’t end up making this sorting error?

To make sure you don’t fall prey to this issue, you need to check your dataset before sorting it.

Select the entire dataset before sorting the data.

You can do that by selecting any cell in the dataset and then using the keyboard shortcut – Control + A.

If there are blank rows/columns in your dataset, the entire dataset won’t get selected. You can identify this by quickly scanning the outline of the selection.

If you see that there is some data left outside the selection, you can manually select it.

To make sure this issue is avoided, it’s best to first unhide all hidden rows and columns and then proceed to sort the data by selecting the entire data set or by first deleting the blank rows.

Partial Sorting (Based on the Last Name)

Sometimes, you may have a dataset that you want to sort based on part of the text.

For example, suppose I have the dataset as shown below and I want to sort this data based on the last name.

Sort Data based on last name - dataset

To do this, I need to separate this data so that I have only the last names in one column. When I have it, I can use that column to sort the data.

Below is a formula that can give me the last name:

=RIGHT(B2,LEN(B2)-FIND(" ",B2))

This will give you a result as shown below.

Formula to get the last name

Now you can use the Last Name column to sort this data.

Once done, you can delete the last name column or hide it.

Note that the above formula works when you have names that has first and last name only and have no double spaces in between. In case you have names data that is different, you will have to adjust the formula. You can also use Text to Column to quickly split the names based on space character as the delimiter.

This is just one example of sorting based on partial data. Other examples may include sorting based on cities in an address or employee ID based on department codes.

Also, in case the text based on which you need to sort is at the beginning of the text, you can use the normal sort feature only.

Other Sorting Examples (Using Formula and VBA)

So far in this tutorial, I have covered the examples where we have used the in-built sorting functionality in Excel.

Automatically Sorting Using a Formula

When you use the in-built sort functionality and then make any change in the data set, you need to sort it again. The sort functionality is not dynamic.

In case you want to get a dataset that sorts automatically whenever there is any change, you can use the formulas to do it.

Note that to do this, you need to keep the original dataset and the sorted dataset separate (as shown below)

I have a detailed tutorial where I cover how to sort alphabetically using formulas. It shows two methods to do this – using helper columns and using an array formula.

Excel has also introduced the SORT dynamic array formula which can easily do this (without helper column or a complicated array formula). But since this is quite new, you may not have access to it in your version of Excel.

Sorting Using VBA

And finally, if you want to bypass all the sorting dialog box or other sorting options, you can use VBA to sort your data.

In the below example, I have a dataset that sorts the data when I double click on the column header. This makes it easy to sort and can be used in dashboards to make it more user-friendly.

Here is a detailed tutorial where I cover how to sort data using VBA and create something as shown above.

I have tried to cover a lot of examples to show you different ways you can sort data in Excel and all the things you need to keep in mind when doing it.

Hope you found this tutorial useful.

You May Also Like the following Excel Tutorials:

  • Search and Highlight Data Using Conditional Formatting
  • How to Sort Worksheets in Excel using VBA
  • How to Create a Data Entry Form in Excel
  • How to Compare Two Columns in Excel
  • Excel TRIM Function to Remove Spaces and Clean Data
  • Flip Data in Excel | Reverse Order of Data in Column/Row
  • How to Shuffle a List of Items/Names in Excel? 2 Easy Formulas!

Worksheet Functions

Excel Functions: Excel 2021 and 365 provide the following sorting and filtering array functions.

SORT(R1, sortcol, order): returns an array with the data in R1 in sorted order based on the elements in column sortcol (the “sort key”) of R1 (default 1). If order = 1 (default), the sort is in ascending order; if order = -1, the sort is in descending order.

UNIQUE(R1): returns an array with the unique entries in the column array R1.

FILTER(R1, criteria, if_empty): returns an array with the subset of the data in R1 that meets the criteria; if none of the data meets the criteria then the if_empty value is returned (if omitted then a #CALC! error value is returned)

SORT Function

E.g. to sort the data in Figure 1 of Sorting and Filtering and place the output in the range starting in cell F4, insert the dynamic array formula =SORT(A4:D12,4) in cell F4 and press Enter. The result is shown in Figure 1.

SORT function

Figure 1 – SORT function

UNIQUE Function

Placing the dynamic array formula =UNIQUE(D4:D12) in cell K4 returns the output shown in column K of Figure 2. Placing the dynamic array formula =SORT(UNIQUE(D4:D12)) in cell M4 returns the output shown in column M of Figure 2.

UNIQUE function

Figure 2 – UNIQUE function

FILTER Function

We can place the formula

=FILTER(A4:D12, (D4:D12>=35000)*(D4:D12<=45000),””)

in cell O4 to extract all the people from Figure 1 who have income between 35,000 and 45,000 inclusive, as shown in Figure 3.

FILTER function with AND

Figure 3 – FILTER function (AND criteria)

Note that we use “*” to specify that two criteria must both hold (i.e. as an AND of the two criteria). We can use “+” to specify that one of two criteria must hold (i.e. as an OR of the two criteria). We can use a combination of one or more “+” and “*” to specify more involved criteria.

We can place the formula

=FILTER(A4:D12,(D4:D12>40000)+(B4:B12=“F”),””)

in cell T4 to extract all the entries from Figure 1 of people who have income that exceeds 40,000 or are female, as shown in Figure 4.

FILTER function with OR

Figure 4 – FILTER function (OR criteria)

Note that the criteria expression must refer to a complete row or column or subrange of the range specified in the first argument in FILTER. Thus, while you can use =FILTER(A4:D12,D4:D12>40000) you can’t use =FILTER(A4:D12,D4:D10>40000).

SORTBY Function

Excel Function: Excel 2021 and 365 also provide the following enhanced sorting function:

SORTBY(R1, sortcol1, order1, sortcol2, order2, …): returns an array with the data in R1 in sorted order based on the elements in the column array sortcol1 (the “sort key”) of R1 (default 1). If order1 = 1 (default), the sort is in ascending order; if order1 = -1, the sort is in descending order. If specified via sortcol2 you can also use a secondary sort key (and additional sort keys  sortcol3, etc. if you like).

Whereas the sortcol argument in SORT is the index to a column in R1, in SORTBY sortcol1 is a column array or range that may or may not be part of R1, although it must have the same number of rows as R1.

E.g. if you want to sort the data in Figure 1 by income and then by the person’s name (as in Figure 5 of Sorting and Filtering), you can enter the formula =SORTBY(A4:D12,D4:D12,1,A4:A12,1) in cell Y4 and press Enter, as shown in Figure 5.

SORTBY function

Figure 5 – SORTBY function

If you want to sort first by age in ascending order and then by income in descending order but you don’t want the incomes to appear in the output, you can place the formula =SORTBY(A4:C12,C4:C12,1,D4:D12,-1) in cell AD4 and press Enter, as shown in Figure 6.

Another SORTBY example

Figure 6 – Another SORTBY example

References

Mellor, L. (2020) Sort and filter using dynamic array functions in Microsoft Excel
https://www.laramellortraining.co.uk/how-to-sort-and-filter-using-dynamic-array-functions-in-microsoft-excel

Stack Overflow (2021) Sortby function combined with Unique combined with filter functions
https://stackoverflow.com/questions/68246608/sortby-function-combined-with-unique-and-filter-functions-how-to-use-it-correcl

Proctor, M. (2020) SORTBY function in Excel. Excel off the Grid
https://exceloffthegrid.com/sortby-function-in-excel/

The excel sort function is one of the most useful new features in Excel. It is one of many functions that make advantage of Excel’s new dynamic array calculation engine, which enables Excel to spill results from a single formula across numerous cells.

Table of Contents

  • SORT function in Excel
  • Notes on use
  • Excel sort formula – Tips and Tricks
  • Excel’s SORT Function Constraints
  • Arrange and Rank
  • The SORT Feature
    • Related Functions

The SORT function in Excel sorts the contents of an array or range alphabetically or numerically by columns or rows.

SORT is a member of the family of dynamic array functions. As a consequence, a dynamic array is created that overflows vertically or horizontally onto nearby cells, depending on the form of the source array.

The SORT function has the following syntax:

=SORT(array, [sort index], [sort order], [by col])

Where:

  • Array (needed) – represents a collection of values or a range of cells to sort. These may be any kind of value, such as text, numbers, dates, or timings.
  • Sort index (optional) – an integer indicating the column or row from which to sort. If this parameter is missing, the default index 1 is used.
  • Sort order (optional) – specifies the order in which items are sorted:
  • 1 or omitted (default) – ascending order, that is, from smallest to greatest -1 – descending order, that is, from greatest to smallest
  • By col (optional) – a logical value indicating the sort direction:
  • FALSE or omitted (by default) – row-based sorting. This is the most often used option.
  • TRUE – column-based sorting. This option is appropriate if your data is structured horizontally in columns.

Notes on use

The excel sort formula uses a formula to organize the contents of a range or array in ascending or descending order. The outcome of SORT is a dynamic array of data that “spill” into a range on the worksheet. If the values in the source data change, the SORT result will immediately update.

The SORT function accepts four parameters: an array, a sort index, a sort order, and a by col parameter. The first option, array, specifies the array or range to sort. This is the only argument that is necessary. By default, the SORT method sorts data upward using the array’s first column. Utilize the optional sort index and sort order parameters to specify which column to sort by and in what order to sort (ascending or descending). Sort index should be an integer that corresponds to the sorting column (or row). For instance, to sort by the third column in a set of data, use the sort index 3 parameter. The optional sort order parameter specifies the direction of the sort. Ascending order is 1 and descending order is -1. The SORT function by default sorts data vertically by row. Set the fourth option, by col, to TRUE to sort a range horizontally by columns.

Excel sort formula – Tips and Tricks

Excel sort function is a novel dynamic array function, and as such, it has a few quirks worth noting:

Currently, only Microsoft 365 and Excel 2021 support the excel sort function. Because Excel 2019 and Excel 2016 do not support dynamic array formula, they do not have the SORT function.

If the array generated by a SORT formula is not provided to another function, Excel dynamically builds and populates a suitably sized range with the sorted data. As a result, always ensure that there are sufficient empty cells below or to the right of the cell where the formula is entered; otherwise, a #SPILL error occurs.

The findings are continuously updated in real time as the underlying data changes. The array given to the formula, on the other hand, does not immediately expand to accept additional elements inserted outside of the referred array. To include such things, you must either change the array reference in your formula, convert the source range to a table, or build a dynamic named range.

Excel’s SORT Function Constraints

The SORT function cannot be used to sort columns or rows that are not contiguous.

For instance, you cannot sort the employee names and wages separately since they are not adjacent.

However, you may sort by employee name and date of hire, or by employee name and date of hire and salary.

If you ever need to sort non-adjacent columns, you may use the approach outlined in this article.

If you wish to sort any range of cells according to a row or column outside that range, you cannot use the SORT function.

For instance, you cannot use the SORT function to sort just the employee names and joining dates by salary.

To resolve these sorts of issues, you may utilize Excel‘s SORTBY function. Alternatively, follow the process outlined in this article.

Arrange and Rank

In this article, we’ll examine one Dynamic Array Functions: SORT. It is excellent for showing a list in a ranked or chronological sequence (date, time).

In our example, we’re going to classify workers according to the amount of time they spend in the office (duration) over the course of a week. This will rank the workers from those who spent the greatest time at work to those who spent the least.

However, before we begin sorting, allow me to demonstrate quickly how I combined all of the duration periods for each employee using the SUMIF function.

The SORT Feature

Along with ordering the personnel from most to least hours worked, it would be beneficial to provide the matching hours next to each name. There are other methods to achieve this, however we will utilize the SORT function. This is another Dynamic Array Function that returns a wide range of values.

To use SORT, you only need to provide the range to sort and the order in which it should be sorted. (The SORT function’s second parameter, sort index, is not required in this case.)

Our straightforward formula would be as follows:

=SORT(B2:B8,,-1)

excel sort function1


  • Excel Filter function
    The Excel  FILTER function extracts matched records from a collection of data using one or more logical checks. The include argument specifies logical tests, which might encompass a wide variety of formula conditions.==FILTER(array,include,[if empty])…

Like this post? Please share to your friends:
  • Order by alphabet word
  • Orc pdf в word онлайн
  • Oral b braun 3d excel
  • Oracle отчеты в excel
  • Oracle to excel vba