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 files which support this post, as you’ll be able to work along with examples. This is the best way to learn. You’ll be able to see the solutions in action, plus the file will be useful for future reference. The support files are available for FREE to newsletter subscribers.

Click below to subscribe and gain access to the subscriber area. You will also receive:

- My favorite tips and tricks direct to your inbox
- Exclusive content (which is only available to subscribers)
- FREE tools and downloads

If you’re already a subscriber, **click here to log-in **to the subscriber downloads area.

The filename for this post is **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.

**Download the 100 Excel Macros ebook**

- Contains 100 Excel VBA macros
- Learn VBA by following along with the example codes
- Apply to your macros, automate Excel, save time.

**Download the ebook today!**

Click the button below to subscribe, you’ll gain access to the subscriber area in which you can download the ebook.

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

**Don’t forget:**

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

**Do you need help adapting this to your needs?**

I’m guessing the examples in this post didn’t exactly meet 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: