Table of Contents
Excel SORT function
Excel SORT function - tips and notes
Basic Excel SORT formula
Formula to sort in ascending order
Formula to sort in descending order
How to sort data in Excel using formula
Excel SORT by column
Sort by multiple columns in different order (multi-level sort)
Sort and filter in Excel
Get N largest or smallest values and sort the results
Return a sorted value in a specific position
Use Excel table to get sort array to expand automatically
Excel SORT function not working
#NAME error: older Excel version
#SPILL error: something blocks spill range
#VALUE error: invalid arguments
#REF error: source workbook is closed
Practice workbook for download
Home Topics excel Excel SORT function - auto sort data using formula

Excel SORT function - auto sort data using formula

Apr 01, 2025 am 10:14 AM

The tutorial shows how to use the SORT function to sort data arrays dynamically. You will learn a formula to sort alphabetically in Excel, arrange numbers in ascending or descending order, sort by multiple columns, and more.

The Sort functionality has been around for a long time. But with the introduction of dynamic arrays in Excel 365, there appeared an amazingly simple way to sort with formulas. The beauty of this method is that the results update automatically when the source data changes.

Excel SORT function

The SORT function in Excel sorts the contents of an array or range by columns or rows, in ascending or descending order.

SORT belongs to the group of Dynamic array functions. The result is a dynamic array that automatically spills to neighboring cells vertically or horizontally, depending on the shape of the source array.

The syntax of the SORT function is as follows:

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

Where:

Array (required) - is an array of values or a range of cells to sort. These can be any values including text, numbers, dates, times, etc.

Sort_index (optional) - an integer that indicates which column or row to sort by. If omitted, the default index 1 is used.

Sort_order (optional) - defines the sort order:

  • 1 or omitted (default) - ascending order, i.e. from smallest to largest
  • -1 - descending order, i.e. from largest to smallest

By_col (optional) - a logical value that indicates the direction of sorting:

  • FALSE or omitted (default) - sort by row. You'll use this option most of the time.
  • TRUE - sort by column. Use this option if your data is organized horizontally in columns like in this example.

Excel SORT function - tips and notes

SORT is a new dynamic array function and as such it has a couple of specificities that you should be aware of:

  • Currently the SORT function is only available in Microsoft 365 and Excel 2021. Excel 2019, Excel 2016 do not support dynamic array formulas, so the SORT function is not available in these versions.
  • If the array returned by a SORT formula is the final result (i.e. not passed to another function), Excel dynamically creates an appropriately sized range and populates it with the sorted values. So, be sure you always have enough empty cells down or/and to the right of the cell where you enter the formula, otherwise a #SPILL error occurs.
  • The results update dynamically as the source data changes. However, the array supplied to the formula does not extend automatically to include new entries that are added outside of the referenced array. To include such items, you need to either update the array reference in your formula, or convert the source range to an table as shown in this example, or create a dynamic named range.

Basic Excel SORT formula

This example shows a basic formula for sorting data in Excel in ascending and descending order.

Supposing your data is arranged alphabetically as shown in the screenshot below. You are looking to sort numbers in column B without breaking or mixing data.

Formula to sort in ascending order

To sort values in column B from smallest to largest, here's the formula to use:

=SORT(A2:B8, 2, 1)

Where:

  • A2:B8 is the source array
  • 2 is the column number to sort by
  • 1 is the ascending sort order

Since our data is organized in rows, the last argument can be omitted to default to FALSE - sort by rows.

Just enter the formula in any empty cell (D2 in our case), press Enter, and the results will spill automatically to D2:E8.

Excel SORT function - auto sort data using formula

Formula to sort in descending order

To sort data descending, i.e. from largest to smallest, set the sort_order argument to -1 like this:

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

Enter the formula in the top left cell of the destination range and you will get this result:

Excel SORT function - auto sort data using formula

In a similar manner, you can sort text values in alphabetical order from A to Z or from Z to A.

How to sort data in Excel using formula

The below examples show a few typical uses of the SORT function in Excel and a couple of non-trivial ones.

Excel SORT by column

When you sort data in Excel, for the most part you change the order of rows. But when your data is organized horizontally with rows containing labels and columns containing records, you might need to sort from left to right, rather than from top to bottom.

To sort by column in Excel, set the by_col argument to TRUE. In this case, sort_index will represent a row, not a column.

For example, to sort the below data by Qty. from highest to lowest, use this formula:

=SORT(B1:H2, 2, 1, TRUE)

Where:

  • B1:H2 is the source data to sort
  • 2 is the sort index, since we are sorting numbers in the second row
  • -1 indicates the descending sort order
  • TRUE means to sort columns, not rows

Excel SORT function - auto sort data using formula

Sort by multiple columns in different order (multi-level sort)

When working with complex data models, you may often need a multi-level sort. Can that be done with a formula? Yep, easily! What you do is to supply array constants for the sort_index and sort_order arguments.

For example, to sort the below data first by Region (column A) from A to Z, and then by Qty. (column C) from smallest to largest, set the following arguments:

  • Array is the data in A2:C13.
  • Sort_index is the array constant {1,3}, since we first sort by Region (1st column), and then by Qty. (3rd column).
  • Sort_order is the array constant {1,-1}, since the 1st column is to be sorted in ascending order and the 3rd column in descending order.
  • By_col is omitted because we sort rows, which is default.

Putting the arguments together, we get this formula:

=SORT(A2:C13, {1,3}, {1,-1})

And it works perfectly! The text values in the first column are sorted alphabetically and the numbers in the third column from largest to smallest:

Excel SORT function - auto sort data using formula

Sort and filter in Excel

In case when you are looking to filter data with some criteria and put the output in order, use the SORT and FILTER functions together:

SORT(FILTER(array, criteria_range=criteria), [sort_index], [sort_order], [by_col])

The FILTER function gets an array of values based on the criteria you define and passes that array to the first argument of SORT.

The best thing about this formula is that it also outputs the results as a dynamic spill range, without you having to press Ctrl Shift Enter or guess at how many cells to copy it to. As usual, you type a formula in the upper most cell and hit the Enter key.

As an example, we are going to extract items with quantity equal to or greater than 30 (>=30) from the source data in A2:B9 and arrange the results in ascending order.

For this, we first set up the condition, say, in cell E2 as shown in the image below. And then, build our Excel SORT formula in this way:

=SORT(FILTER(A2:B9, B2:B9>=E2), 2)

Apart from array generated by the FILTER function, we only specify the sort_index argument (column 2). The remaining two arguments are omitted because the defaults work exactly as we need (sort ascending, by row).

Excel SORT function - auto sort data using formula

Get N largest or smallest values and sort the results

When analyzing huge bulks if information, there is often a need to extract a certain number of top values. Maybe not just extract, but also arrange them in the desired order. And ideally, choose which columns to include in the results. Sounds tricky? Not with the new dynamic array functions!

Here is a generic formula:

INDEX(SORT(…), SEQUENCE(n), {column1_to_return, column2_to_return, …})

Where n is the number of the values you want to return.

From the below data set, assume you want to get a top 3 list based on the numbers in column C.

To have it done, you first sort the array A2:C13 by the 3rd column in descending order:

SORT(A2:C13, 3, -1)

And then, nest the above formula in the first (array) argument of the INDEX function to have the array sorted from highest to smallest.

For the second (row_num) argument, which indicates how many rows to return, generate the required sequential numbers by using the SEQUENCE function. As we need 3 top values, we use SEQUENCE(3), which is the same as supplying a vertical array constant {1;2;3} directly in the formula.

For the third (col_num) argument, which defines how many columns to return, supply the column numbers in the form of a horizontal array constant. We want to return columns B and C, so we use the array {2,3}.

Eventually, we get the following formula:

=INDEX(SORT(A2:C13, 3, -1), SEQUENCE(3), {2,3})

And it produces exactly the results we want:

Excel SORT function - auto sort data using formula

To return 3 bottom values, simply sort the original data from smallest to largest. For this, change the sort_order argument from -1 to 1:

=INDEX(SORT(A2:C13, 3, 1), SEQUENCE(3), {2,3})

Excel SORT function - auto sort data using formula

Return a sorted value in a specific position

Looking from another angle, what if you only want to return a specific sort position? Say, only the 1st , only the 2nd, or only the 3rd record from the sorted list? To have it done, use the simplified version of the INDEX SORT formula discussed above:

INDEX(SORT(…), n, {column1_to_return, column2_to_return, …})

Where n is the position of interest.

For example, to get a particular position from top (i.e. from the data sorted descending), use this formula:

=INDEX(SORT(A2:C13, 3, -1), F1, {2,3})

To get a specific position from bottom (i.e. from the data sorted ascending), use this one:

=INDEX(SORT(A2:C13, 3, 1), I1, {2,3})

Where A2:C13 is source data, F1 is the position from top, I1 is the position from bottom, and {2,3} are the columns to be returned.

Excel SORT function - auto sort data using formula

Use Excel table to get sort array to expand automatically

As you already know, the sorted array updates automatically when you make any changes to the original data. This is the standard behavior of all dynamic array functions, including SORT. However, when you add new entries outside the referenced array, they are not automatically included in a formula. If you'd like your formula to respond to such changes, convert the source range to a fully-functional Excel table and use structured references in your formula.

To see how it works in practice, please consider the following example.

Supposing you use the below Excel SORT formula to arrange values in the range A2:B8 in alphabetical order:

=SORT(A2:B8, 1, 1)

Then, you input a new entry in row 9… and are disappointed to see that the newly added entry is left out of the spill range:

Excel SORT function - auto sort data using formula

Now, convert the source range to a table. For this, simply select your range including the column headers (A1:B8) and press Ctrl T. When building your formula, select the source range using the mouse, and the table name will be inserted in the formula automatically (this is called a structured reference):

=SORT(Table1, 1, 1)

When you type a new entry right below the last row, the table will expand automatically, and the new data will be included in the spill range of the SORT formula:

Excel SORT function - auto sort data using formula

Excel SORT function not working

If your SORT formula results in an error, it's most likely because of the following reasons.

#NAME error: older Excel version

SORT is a new function and works only in Excel 365 and Excel 2021. In older versions where this function is not supported, a #NAME? error occurs.

#SPILL error: something blocks spill range

If one or more cells in the spill range are not completely blank or merged, a #SPILL! error is displayed. To fix it, just remove the blockage. For more information, please see Excel #SPILL! error - what it means and how to fix.

#VALUE error: invalid arguments

Whenever you run into a #VALUE! error, check the sort_index and sort_order arguments. Sort_index should not exceed the number of columns is array, and sort_order should be either 1 (ascending) or -1 (descending).

#REF error: source workbook is closed

Since dynamic arrays have limited support for references between workbooks, the SORT function requires both files to be open. If the source workbook is closed, a formula will throw a #REF! error. To fix it, just open the referenced file.

That's how to sort data in Excel using formula. I thank you for reading and hope to see you on our blog next week!

Practice workbook for download

Sorting in Excel with formulas (.xlsx file)

The above is the detailed content of Excel SORT function - auto sort data using formula. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Excel formula to find top 3, 5, 10 values in column or row Excel formula to find top 3, 5, 10 values in column or row Apr 01, 2025 am 05:09 AM

This tutorial demonstrates how to efficiently locate the top N values within a dataset and retrieve associated data using Excel formulas. Whether you need the highest, lowest, or those meeting specific criteria, this guide provides solutions. Findi

Add a dropdown list to Outlook email template Add a dropdown list to Outlook email template Apr 01, 2025 am 05:13 AM

This tutorial shows you how to add dropdown lists to your Outlook email templates, including multiple selections and database population. While Outlook doesn't directly support dropdowns, this guide provides creative workarounds. Email templates sav

How to use Flash Fill in Excel with examples How to use Flash Fill in Excel with examples Apr 05, 2025 am 09:15 AM

This tutorial provides a comprehensive guide to Excel's Flash Fill feature, a powerful tool for automating data entry tasks. It covers various aspects, from its definition and location to advanced usage and troubleshooting. Understanding Excel's Fla

Regex to extract strings in Excel (one or all matches) Regex to extract strings in Excel (one or all matches) Mar 28, 2025 pm 12:19 PM

In this tutorial, you'll learn how to use regular expressions in Excel to find and extract substrings matching a given pattern. Microsoft Excel provides a number of functions to extract text from cells. Those functions can cope with most

How to add calendar to Outlook: shared, Internet calendar, iCal file How to add calendar to Outlook: shared, Internet calendar, iCal file Apr 03, 2025 am 09:06 AM

This article explains how to access and utilize shared calendars within the Outlook desktop application, including importing iCalendar files. Previously, we covered sharing your Outlook calendar. Now, let's explore how to view calendars shared with

FV function in Excel to calculate future value FV function in Excel to calculate future value Apr 01, 2025 am 04:57 AM

This tutorial explains how to use Excel's FV function to determine the future value of investments, encompassing both regular payments and lump-sum deposits. Effective financial planning hinges on understanding investment growth, and this guide prov

MEDIAN formula in Excel - practical examples MEDIAN formula in Excel - practical examples Apr 11, 2025 pm 12:08 PM

This tutorial explains how to calculate the median of numerical data in Excel using the MEDIAN function. The median, a key measure of central tendency, identifies the middle value in a dataset, offering a more robust representation of central tenden

How to remove / split text and numbers in Excel cell How to remove / split text and numbers in Excel cell Apr 01, 2025 am 05:07 AM

This tutorial demonstrates several methods for separating text and numbers within Excel cells, utilizing both built-in functions and custom VBA functions. You'll learn how to extract numbers while removing text, isolate text while discarding numbers

See all articles