The SORTBY function was announced by Microsoft on 24 September 2018 and is one of Excel’s new dynamic array functions. SORTBY makes use of the changes made to the calculation engine, enabling Excel to return (or “Spill” to use the correct terminology) the results of a single formula into multiple cells.

At the time of writing, Microsoft has only made this new functionality available to Office 365 subscribers on the Insider channel. The updates will be rolled out to all Office 365 subscribers at a future point, but first Microsoft need to assess the impact the changes to the calculation engine will have. The new dynamic array functions and features are not available in Excel 2019 or previous versions, so it is definitely worth getting an Office 365 subscription, otherwise, you will be waiting until Excel 2022, and that’s just too long to wait!

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. That’s pretty useful, right?

## Arguments of the SORTBY function

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

## 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. The total Total Value column (Cells E3-E10) is not within the array, but it is still possible to use it as the sort column.

### Example 2 – SORTBY retains the semantic column names

Example 2 shows the advantages of using SORTBY with an Excel Table.

The formula in Cell G3 is:

=SORTBY(salesTable[[Employee]:[Average Price]],salesTable[Total Value],-1)

The second argument in the SORTBY function determines the values to sort by. When linked to a Table this column can have a meaningful name. In the example above we are sorting by the *salesTable[Total Value]* column. The ability to use descriptive column names is an advantage over the SORT function which sorts based on an index column within the array.

### Example 3 – SORTBY expands automatically when linked to a Table

This example shows how the SORTBY function responds when linked 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.

### Example 4 – Using SORTBY with multiple columns

Example 4 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. That’s much better!

### Example 5 – Combining FILTER and SORTBY

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

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 sorts the data, but the second argument is working on 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 wanted 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 which is returning the 3rd item in the list.

## Want to learn more?

There is a lot to learn about dynamic arrays and the new array 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

Also, here are some other resources you might find useful:

- Ebook – Dynamic arrays straight to the point – By Bill Jelen (free until December 2018)
- Video – Comprehensive dynamic array formulas: The power of dynamic arrays
- Blog Post – Dynamic array formulas & spill ranges