The SORTBY function was announced by Microsoft in September 2018 and is one of Excel’s new dynamic array functions. SORTBY makes use of the changes made to the calculation engine, enabling a single formula to spill calculations into multiple cells.

At the time of writing, the SORTBY function is only available to those on a Microsoft 365 subscription. It will not be available in Excel 2019 or earlier versions.

**Download the example file**

I recommend you download the example file for this post. Then you’ll be able to work along with examples and see the solution in action, plus the file will be useful for future reference.

Download the file: 0034 SORTBY function in Excel.zip

**Watch the video:**

Contents

## Arguments of the SORTBY function

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

The video below is sorting the Employees based on the Units Sold; this is what SORTBY does. The range returned does not need to include the values that are being sorted by. That’s pretty useful, right?

SORTBY has a variable number of arguments depending on your scenario:

=SORTBY(array, By_array1, [sort_order1], [By_array2], [sort_order2] ,...)

**array:**The range of cells, or array of values to be returned by the function.**By_array1:**The range of cells or array of values to sort by.**[sort_order1]:**1 = sort*By_array1*in ascending order, -1 = sort*By_array1*in descending order (if excluded it will default to 1).**[By_array2…]:**The range of cells or array of values to apply the second sort by. This argument is entirely optional; you can exclude this if you only need one sort column.**[sort_order2]:**the sort order to apply to the*By_array2.*1= ascending, -1 = descending.

If there is a third, fourth or nth sort required, these can be added just like *By_array2* and *sort_order2*.

Only the first two arguments are necessary, which are the data and what to sort by. If you don’t need to sort by a separate column, then the SORT function may be better suited to your requirements.

## Examples of using the SORTBY function

The following examples illustrate how to use the SORTBY function.

### Example 1 – The sort column does not need to be in the array

In this example, the Employees, Units Sold and Average Price columns are returned based on the descending order of the values in the Total Value column.

The formula in cell G3 is:

=SORTBY(B3:D10,E3:E10,-1)

Cells B3-D10 are sorted by the values in E3-E10 in descending order (as the third argument in the function is -1). The Total Value column (cells E3-E10) is not within the result, but it is still possible to use it as the sort column.

### Example 2 – SORTBY expands automatically when linked to a table

This example shows how the SORTBY function responds when new data is added to an Excel table.

The SORTBY function is using a table called *salesTable* as its source. New records added to the table are automatically added to the spill range of the function. There is no need to expand the range of the function; it happens all by itself.

### Example 3 – Using SORTBY with multiple columns

Example 3 shows how to sort using multiple columns.

The formula in cell G3 is:

=SORTBY(B3:D10,C3:C10,-1,B3:B10,1)

Cells B3-D10 are sorted first by cells C3-C10 (the Units Sold) in descending order, then by cells B3-B10 (the Employee name) in ascending order.

### Example 4 – Returning columns in any order when using SORTBY

SORTBY accepts a range or array as the first argument. It then returns the columns in the result in the same order. But what if we want a different order, or only wish to return a few of the columns? In this circumstance, we can use the CHOOSE function to create a range of cells in any order).

The formula in cell G3 is:

=SORTBY(CHOOSE({1,2},E3:E10,B3:B10),C3:C10,-1,B3:B10,1)

The CHOOSE function is using E3-E10 as the first range, and B3-B10 as the second range. The {1,2} is telling the CHOOSE function which position each range should be in. If you were to use {2,1}, the ranges would be returned in the reverse order.

Using this method, we can return columns in any order; we are not restricted by the layout of the source data.

### Example 5 – Combining FILTER and SORTBY

The dynamic array functions can be nested within each other. But this nesting can bring some challenges. This example shows the FILTER function nested within SORTBY.

The formula in cell G3 is:

=FILTER(SORTBY(B3:D10,C3:C10,1),C3:C10>5)

This formula is intended to sort based on cells C3-C10, then filter to only return the rows where the values in C3-C10 are greater than 5.

But did you notice in the screenshot that it doesn’t return the correct values? This occurs because the first argument of the FILTER function uses SORTBY to sort, but the second argument is still working on the unsorted data. When nesting these formulas, we need to apply the sort to each argument.

Let’s try it again…

The formula in cell G3 is:

=FILTER(SORTBY(B3:D10,C3:C10,1),SORTBY(C3:C10,C3:C10,1)>5)

Now both arguments of the FILTER function are based on arrays sorted by C3-C10.

### Example 6 – Restrict the values returned by SORTBY

Finally, what if you only want to return a single sort position? For example, what if we wished to only the 3rd item from the sorted list?

The formula in cell G3 is:

=INDEX(SORTBY(B3:D10,C3:C10,1),3,0)

SORTBY is nested within the INDEX function. It is the INDEX function that is returning the 3rd item in the list.

### 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

**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:

- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
- 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.
- 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:

Hello,

As you know only 365 users can enjoy the benefits of the Six New Dynamic Array Formulas such as Sortby …

Is there an UDF or an Array Formula which could replicate the basic features of Sortby ( without the Spill effect …) ???

This would be extremely handy for the ” old – loyal ” Excel customer base

Thanks for your help

No there aren’t any non 35 alternatives.