Excel - SORTBY Function



SORTBY Function

The Excel SORTBY function permits users to sort an array or specific cell range based on the values in different array/cell ranges. The SORYBY is supported in Excel 365 and 2021 and is categorized in the Dynamic array functions in Microsoft Excel. The SORT_BY function is not supported in the older versions of Microsoft Excel. For example, you can sort the names of underprivileged kids and their classes based on their age.

Compatibility

This dynamic array function is compatible with the following versions of Microsoft Excel −

  • Excel for Microsoft 365
  • Excel for Microsoft 365 for Mac
  • Excel for the web
  • Excel 2024
  • Excel 2024 for Mac
  • Excel 2021
  • Excel 2021 for Mac
  • Excel for iPad
  • Excel for iPhone
  • Excel for Android tablets
  • Excel for Android phones

Syntax

The syntax of the SORTBY function is as follows −

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

Arguments

You can use the following arguments with the SORTBY function −

Argument Description Required / Optional
Array It specifies an array or range of cells. Required
by_array1 Another array/cell range employed for sorting Optional
sort_order1 Its value can be 1 or -1. You can specify 1 for ascending order and -1 for descending order. Optional
by_array2 The second array/range of cells to sort the array specified in the first argument of the SORT function. Optional
sort_order2 Its value can be -1(for descending order) and 1 for (ascending order) Optional

Points to Remember

  • The arguments sort_order 1 and sort_order 2 must have an equal number of rows and columns; otherwise, the SORTBY function will return the #VALUE! Error.
  • If you specify numeric values other than -1 and 1 in the sort_order1 and sort_order2, then in this case, the SORTBY function will return #VALUE! Error.
  • The dynamic array result will automatically change whenever you modify the data in the defined array.
  • Sometimes, Microsoft Excel will display the #SPILL error if limited rows/columns are available to showcase the result. Make sure you have enough space horizontally and vertically to populate the resultant dynamic array.

Examples of SORTBY Function

Practice the following examples to learn the use of the SORTBY function in Excel.

Example 1

The SORTBY function shortlists the data defined in the provided array, relying on the values in a specified range. In this example, users must shortlist the employee's name by their Salary.

Step 1 − The following datasets are utilized to use the SORTBY function −

SORTBY Function in Excel

Step 2 − Enter the formula "=SORTBY(Table15,D2:D8)" in the F2 cell. Then, press the Enter tab.

SORTBY Function in Excel 1

Explanation

  • The first parameter array represents the Table15 having range A21B8.
  • The second argument by_array1 indicates the cell range D2:D8, excluding its heading.
  • Table 15 is sorted according to their age. For example, if the employee Raayn is the minimum age, he will be in the first position, and so on in the dynamic array.

Step 3 − The SORTBY function will obtain the resulting dynamic array arranged by the children's ages in ascending order.

SORTBY Function in Excel 2

Step 4 − If you wish to display the TABLE15 according to their age in descending order, you can enter the formula "=SORTBY(Table15,D2:D8,-1)" in the F2 cell and hit the Enter tab. Here, the third argument is -1, used for descending order.

SORTBY Function in Excel 3

SORTBY Function in Excel 4

Example 2

Multiple ranges of columns to sort the dataset using the SORTBY function in Excel.

Solution

Assume the sample dataset consists of four columns: Customer, Data Resources, Active/Inactive customers, and Resources popularity.

SORTBY Dataset using Function in Excel

Enter the formula "=SORTBY(B2:B8,A2:A8,1,D2:D8,-1)" in the F2 cell and hit the Enter button. In this formula, the second argument by_array1 specifies the range A2:A8the third argument 1 sorts in ascending form. The fourth argument, by_array2, denotes the cell range D2:D8. The fifth argument, -1, sorts in the descending form.

SORTBY Dataset using Function in Excel 1

Therefore, the SORTBY function displayed the values of the Data Resources column based on the two columns named Customer_id and Resource popularity.

SORTBY Dataset using Function in Excel 2
Advertisements