Excel SORTBY function - custom sort with formula (2024)

Today we'll take a close look at the syntax and typical uses of the new dynamic array SORTBY function. You will learn how to custom sort in Excel with a formula, randomly sort a list, arrange cells by text length, and more.

Microsoft Excel provides a number of ways to arrange text data alphabetically, dates chronologically, and numbers from smallest to largest or from highest to lowest. There is also a way to sort by your own custom lists. In addition to the conventional Sort functionality, Excel 365 introduces a brand-new way to sort data with formulas - very convenient and incredibly simple to use!

Excel SORTBY function

The SORTBY function in Excel is designed to sort one range or array based on the values in another range or array. Sorting can be done by one or multiple columns.

SORTBY is one of six new dynamic array functions available in Excel for Microsoft 365 and Excel 2021. Its result is a dynamic array that spills to neighboring cells and updates automatically when the source data changes.

The SORTBY function has a variable number of arguments - the first two are required and the other are optional:

SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2],…)

Array (required) - the range of cells or array of values to be sorted.

By_array1 (required) - the range or array to sort by.

Sort_order1 (optional) - the sorting order:

  • 1 or omitted (default) - ascending
  • -1 - descending

By_array2 / Sort_order2, … (optional) - additional array / order pairs to use for sorting.

Important note! Currently the SORTBY function is only available with Microsoft 365 subscriptions and Excel 2021. In Excel 2019, Excel 2016 and earlier versions the SORTBY function is not available.

SORTBY function - 4 things to remember

For an Excel SORTBY formula to work correctly, there are a few important points to take notice of:

  • By_array arguments should be either one row high or one column wide.
  • The array and all by_array arguments must have compatible dimensions. For example, when sorting by two columns, array, by_array1 and by_array2 should have the same number of rows; otherwise a #VALUE error will occur.
  • If the array returned by SORTBY is the final result (output in a cell and not passed to another function), Excel creates a dynamic spill range and populates it with the results. So, be sure you have enough empty cells down and/or to the right of the cell where you enter the formula, otherwise you'll get a #SPILL error.
  • The results of SORTBY formulas update automatically whenever the source data changes. However, new entries that are added outside of the array referenced in the formula are not included in the results unless you update the array reference. For the referenced array to expand automatically, convert the source range to an Excel table or create a dynamic named range.

Basic SORTBY formula in Excel

Here's a typical scenario of using a SORTBY formula in Excel:

Supposing, you have a list of projects with the Value field. You want to sort the projects by their value on a separate sheet. As other users do not need to see the numbers, you'd rather not include the Value column in the results.

The task can be easily accomplished with the SORTBY function, for which you supply the following arguments:

  • Array is A2:A10 - since you do not wish the Value column to be displayed in the results, you leave it out of the array.
  • By_array1 is B2:B10 - sort by Value.
  • Sort_order1 is -1 - descending, i.e. from highest to lowest.

Putting the arguments together, we get this formula:

=SORTBY(A2:B10, B2:B10, -1)

For simplicity, we use the formula on the same sheet - enter it in D2 and press the Enter key. The results "spill" automatically to as many cells as needed (D2:D10 in our case). But technically, the formula is only in the first cell, and deleting it from D2 will delete all the results.Excel SORTBY function - custom sort with formula (1)

When used on another sheet, the formula takes the following shape:

=SORTBY(Sheet1!A2:A10, Sheet1!B2:B10, -1)

Where Sheet1 is the worksheet containing the original data.

Using SORTBY function in Excel - formula examples

Below you will find a few more examples of using SORTBY, which will hopefully prove useful and insightful.

Sort by multiple columns

The basic formula discussed above sorts data by one column. But what if you need to add one more level of sorting?

Assuming our sample table has two fields, Status (column B) and Value (column C), we wish to sort first by Status alphabetically, and then by Value descending.

To sort by two columns, we just add one more pair of the by_array / sort_order arguments:

  • Array is A2:C10 - this time, we want to include all three columns in the results.
  • By_array1 is B2:B10 - first, sort by Status.
  • Sort_order1 is 1 - sort alphabetically from A to Z.
  • By_array2 is C2:C10 - then, sort by Value.
  • Sort_order2 is -1 - sort from largest to smallest.

As the result, we get the following formula:

=SORTBY(A2:B10, B2:B10, 1, C2:C10, -1)

Which rearranges our data exactly as we instructed it:Excel SORTBY function - custom sort with formula (2)

Custom sort in Excel with a formula

To sort data in a custom order, you can either use Excel's Custom Sort feature or build a SORTBY MATCH formula in this way:

SORTBY(array, MATCH(range_to_sort, custom_list, 0))

Taking a closer look at our data set, you will probably find it more convenient to sort the projects by their status "logically", e.g. by importance, rather than alphabetically.

To have it done, we first create a custom list in the desired sort order (In progress, Completed, On hold) typing each value in a separate cell in the range E2:E4.

And then, using the generic formula above, we supply the source range for array (A2:C10), the Status column for range_to_sort (B2:B10), and the custom list that we created for custom_list (E2:E4).

=SORTBY(A2:C10, MATCH(B2:B10, E2:E4, 0))

As the result, we've got the projects sorted by their status exactly as needed:Excel SORTBY function - custom sort with formula (3)

To sort by custom list in the reverse order, put -1 for the sort_order1 argument:

=SORTBY(A2:C10, MATCH(B2:B10, E2:E4, 0), -1)

And you'll have the projects sorted in the opposite direction:Excel SORTBY function - custom sort with formula (4)

Want to additionally sort records within each status? No problem. Simply, add one more sort level to the formula, say by Value (C2:C10), and define the desired order of sorting, ascending in our case:

=SORTBY(A2:C10, MATCH(B2:B10, E2:E5, 0), 1, C2:C10, 1)Excel SORTBY function - custom sort with formula (5)

A big advantage of the SORTBY formula over Excel's Custom Sort feature is that the formula updates automatically whenever the original data changes, while the feature requires cleaning up and re-sorting with each change.

How this formula works:

As already mentioned, Excel's SORTBY function can only process "sort by" arrays whose dimensions are compatible with the source array. As our source array (C2:C10) contains 9 rows and the custom list (E2:E4) only 3 rows, we cannot supply it directly to the by_array argument. Instead, we use the MATCH function to create a 9-row array:

MATCH(B2:B10, E2:E5, 0)

Here, we use the Status column (B2:B10) as lookup values and our custom list (E2:E5) as lookup array. The last argument is set to 0 to look for exact matches. As the result, we get an array of 9 numbers, each representing the relative position of a given Status value in the custom list:

{1;3;2;1;3;2;2;1;2}

This array goes directly to the by_array argument of the SORTBY function and forces it to place the data in the order corresponding to the elements of the array, i.e. first entries represented by 1's, then entries represented by 2's, and so on.

Random sort in Excel with a formula

In earlier Excel versions, you can do a random sort with the RAND function as explained in this tutorial: How to randomly sort a list in Excel.

In new Excel, you can use a more powerful RANDARRAY function together with SORTBY:

SORTBY(array, RANDARRAY(ROWS(array)))

Where array is the source data that you want to shuffle.

This generic formula works for a list consisting of a single column as well as for a multi-column range.

For example, to randomly sort a list in A2:A10, use this formula:

=SORTBY(A2:A10, RANDARRAY(ROWS(A2:A10)))Excel SORTBY function - custom sort with formula (6)

To shuffle data in A2:C10 keeping the rows together, use this one:

=SORTBY(A2:C10, RANDARRAY(ROWS(A2:C10)))Excel SORTBY function - custom sort with formula (7)

How this formula works:

The RANDARRAY function produces an array of random numbers to be used for sorting, and you pass it in the by_array argument of SORTBY. To specify how many random numbers to generate, you count the number of rows in the source range by using the ROWS function, and "feed" that number to the rows argument of RANDARRAY. That's it!

Note. Like its predecessor, RANDARRAY is a volatile function and it generates a new array of random numbers every time the worksheet is recalculated. As the result, your data will be resorted with each change on the sheet. To prevent auto resorting, you can use the Paste Special > Values feature to replace formulas with their values.

Sort cells by string length

To sort cells by the length of text strings they contain, use the LEN function to count the number of characters in each cell, and supply the calculated lengths to the by_array argument of SORTBY. The sort_order argument can be set to either 1 or -1, depending on the preferred order of sorting.

To sort by text string from smallest to largest:

SORTBY(array, LEN(array), 1)

To sort by text string from largest to smallest:

SORTBY(array, LEN(array), -1)

And here's a formula that demonstrates this approach on real data:

=SORTBY(A2:A7, LEN(A2:A7), 1)

Where A2:A7 are the original cells you'd like to sort by text length in ascending order:Excel SORTBY function - custom sort with formula (8)

SORTBY vs. SORT

In the group of new Excel dynamic array functions, there are two designed for sorting. Below we list the most essential differences and similarities as well as when each is best to be used.

  • Unlike the SORT function, SORTBY does not require the "sort by" array to be part of the source array, nor does it need to appear in results. So, when your task is to sort a range based on another independent array or a custom list, SORTBY is the right function to use. If you are looking to sort a range based on its own values, then SORT is more appropriate.
  • Both functions support multiple levels of sorting and both can be chained together with other dynamic array and conventional functions.
  • Both functions are only available to Excel 365 and Excel 2021 users.

Excel SORTBY function not working

In case your SORTBY formula returns an error, it's most likely because of one of the following reasons.

Invalid by_array arguments

The by_array argument must be a single row or a single column and compatible in size with the array argument. For example, if array has 10 rows, by_array should also include 10 rows. Otherwise a #VALUE! error occurs.

Invalid sort_order arguments

The sort_order arguments can only be 1 (ascending) or -1 (descending). If no value is set, SORTBY defaults to ascending order. If any other value is set, a #VALUE! error is returned. For more information, see When is #VALUE error raised in Excel?

There isn't enough space for results

Like any other dynamic array function, SORTBY spills the results into an automatically resizable and updatable range. If there are not enough empty cells to display all values, a #SPILL! error is thrown.

Source workbook is closed

If a SORTBY formula references another Excel file, both workbooks need to be open. If the source workbook is closed, a #REF! error occurs.

Your Excel version does not support dynamic arrays

When used in a pre-dynamic version of Excel, the SORT function returns a #NAME? error.

That's how to use the SORTBY function in Excel to do custom sort and other things. I thank you for reading and hope to see you on our blog next week!

Practice workbook for download

Excel SORTBY formulas (.xlsx file)

You may also be interested in

  • Excel SORT function - sort data with formulas
  • How to sort in Excel: by column, by row and in custom order
  • How to sort by cell or font color in Excel
  • Sort randomly in Excel: shuffle cells, rows and columns
  • How to alphabetize in Excel
  • How to sort by date in Excel
Excel SORTBY function - custom sort with formula (2024)

FAQs

How do I add a SORTBY formula in Excel? ›

The SORTBY function has a variable number of arguments - the first two are required and the other are optional: SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2],…) Array (required) - the range of cells or array of values to be sorted. By_array1 (required) - the range or array to sort by.

How to auto custom sort in Excel? ›

Follow these steps:
  1. Select the columns to sort. ...
  2. In the ribbon, click Data > Sort.
  3. In the Sort popup window, in the Sort by drop-down, choose the column on which you need to sort. ...
  4. From the Order drop-down, select Custom List.
  5. In the Custom Lists box, select the list that you want, and then click OK to sort the worksheet.

How do I keep a formula constant in Excel when sorting? ›

To keep a cell constant, add the absolute reference to the formula that uses that cell. Once you have created or selected your desired formula in the spreadsheet, select the cell that contains the formula. This allows you to edit the formula in the formula bar.

What is the difference between the sort and SORTBY functions? ›

The SORT function will sort a list but the SORTBY function will sort a list based on a column not included in the sort array.

How to do conditional sorting in Excel? ›

To sort using conditional formatting:
  1. Click on the data in the row you want to sort.
  2. Click on the “Data” tab in your toolbar.
  3. Click on the “Sort” button.
  4. Under “Column,” select the column you want to sort. ...
  5. In the “Sort On”, select “Cell Color.”
  6. In the “Order column,” select the red bar.
Apr 30, 2024

How can you sort data in Excel based on multiple criteria? ›

Sort data in a table
  1. Select Custom Sort.
  2. Select Add Level.
  3. For Column, select the column you want to Sort by from the drop-down, and then select the second column you Then by want to sort. ...
  4. For Sort On, select Values.
  5. For Order, select an option, like A to Z, Smallest to Largest, or Largest to Smallest.

What is the formula for multiple sort in Excel? ›

It can sort by multiple columns. The syntax is: =SORTBY(array, by_array1, [sort_order1], [[by_array2], [sort_order2]],…) array is the range or array containing the values you want sorted.

How do you sort in Excel filter formula? ›

In this case, we're using the FILTER function with the addition operator (+) to return all values in our array range (A5:D20) that have Apples OR are in the East region, and then sort Units in descending order: =SORT(FILTER(A5:D20,(C5:C20=H1)+(A5:A20=H2),""),4,-1).

What is custom sorting? ›

Custom sorting with multiple columns refers to the process of arranging data in a table or dataset based on multiple criteria in a user-defined order. Instead of using a default sorting method, you specify your own rules for sorting data based on different columns in a particular sequence.

How to advanced sort in Excel? ›

Procedure
  1. Open a report.
  2. Click the Sorting icon. on the toolbar, and then click Advanced Sorting.
  3. In the Advanced Sorting dialog, determine which columns you wish to sort by and drag and drop them under the Detail Sort List folder.
  4. You can change the order with the up and down arrows or change the sort direction.

How to use sortby formula in Excel? ›

To sort this data based on two criteria, select an empty cell where you want the sorted data to appear. Then, enter the SORTBY formula arguments: A3:C12 is the range of data you want to sort. Argument 1: B3:B12 is the age range; 1 indicates a descending sort order.

How to sort in Excel using formulas? ›

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.

How do I lock a formula when sorting in Excel? ›

If you want to lock all cells in the spreadsheet with formulas, go to the "Home" tab at the top, open the "Editing" group, click on "Find & Select," use the "Go To Special" button to open the "Go To Special" dialog box, then select the "Formulas" button to select all cells with formulas and finish with "OK." If you ...

How do I filter specific data in Excel using formulas? ›

The FILTER function allows you to filter a range of data based on criteria you define. In the following example we used the formula =FILTER(A5:D20,C5:C20=H2,"") to return all records for Apple, as selected in cell H2, and if there are no apples, return an empty string ("").

What is the formula for SORTBY alphabetical order in Excel? ›

Sort Function: In a nearby cell, you can use the SORT function to sort the values from your original cell alphabetically. For example, if your original data is in cell A1, you can use a formula like =SORT(SPLIT(A1, ",")) to alphabetically sort the comma-separated values.

How do I sort data in Excel by yes or no? ›

Sorting the Column

If you want to sort the table by the data in the yes or no column, use Excel's sort feature. To do this, click the "Data" tab on the ribbon menu. Then, within the "Sort & Filter" group, click the "Sort" button. Choose the column in question in the "Sort by" drop-down.

References

Top Articles
FPGA Engineer II/Senior FPGA Engineer Pipeline | Rocket Lab
ELL Teacher – Middle and High School
Mybranch Becu
7 C's of Communication | The Effective Communication Checklist
It’s Time to Answer Your Questions About Super Bowl LVII (Published 2023)
Dricxzyoki
Comcast Xfinity Outage in Kipton, Ohio
Owatc Canvas
Bbc 5Live Schedule
Insidekp.kp.org Hrconnect
Truck Trader Pennsylvania
50 Shades Darker Movie 123Movies
Clear Fork Progress Book
Cta Bus Tracker 77
Costco Great Oaks Gas Price
Mychart Anmed Health Login
Walgreens Alma School And Dynamite
Selfservice Bright Lending
Encore Atlanta Cheer Competition
25 Best Things to Do in Palermo, Sicily (Italy)
Craigslist Wilkes Barre Pa Pets
Essence Healthcare Otc 2023 Catalog
Www Pointclickcare Cna Login
Meta Carevr
Gen 50 Kjv
Downtown Dispensary Promo Code
Babydepot Registry
Laveen Modern Dentistry And Orthodontics Laveen Village Az
Ni Hao Kai Lan Rule 34
Etowah County Sheriff Dept
Austin Automotive Buda
Troy Gamefarm Prices
Has any non-Muslim here who read the Quran and unironically ENJOYED it?
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
Craigslist Mexicali Cars And Trucks - By Owner
968 woorden beginnen met kruis
Invalleerkracht [Gratis] voorbeelden van sollicitatiebrieven & expert tips
303-615-0055
National Weather Service Richmond Va
Jammiah Broomfield Ig
From Grindr to Scruff: The best dating apps for gay, bi, and queer men in 2024
Gw2 Support Specter
Meet Robert Oppenheimer, the destroyer of worlds
Plumfund Reviews
Yosemite Sam Hood Ornament
German American Bank Owenton Ky
SF bay area cars & trucks "chevrolet 50" - craigslist
Is TinyZone TV Safe?
Ics 400 Test Answers 2022
Overstock Comenity Login
Syrie Funeral Home Obituary
Escape From Tarkov Supply Plans Therapist Quest Guide
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6389

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.