Excel SORTBY Function
The SORTBY function sorts the contents of one range or array based on the values in a corresponding range or array, no matter the corresponding range or array is included in the range or array to be sorted or not.
The SORTBY function returns an array.
- array can be supplied as a row of values, a column of values, or a combination of rows and columns of values.
- A by_array argument should be supplied as one row or one column, and it must have dimensions compatible with array.
- The dimensions of the returned array are the same as the array argument. If one or more cells in the spill range are not empty, the #SPILL! error will be returned.
- If you want to omit the sort_order argument in additional array and sort order pairs, you should keep the comma (,), e.g. [by_array2,].
- SORTBY can be used between different workbooks. However, make sure the referenced workbooks are open. Otherwise, a #REF! error will be returned if you refresh the result cell.
- The results of SORTBY are dynamic, meaning that they update automatically when values in the source data change. However, the results will not be updated if you add new entries to array. To fix this, you should use structured references as array argument.
Suppose that you have score ranking in a table as shown in the screenshot below. To sort the table by classes (from class A to class B) and then by scores (from highest to lowest), and not showing scores, you should set the array argument to the columns you want to show, that is C4:E15, and set by_array1 to the class column and leave the sort_order empty since it defaults to sort Alphabetically. Then you should add an additional array and sort order pair: B4:B15,-1. Please copy or enter the formula below in the top left cell of the result table and then press Enter to get the result: