Sorting and Filtering Functions

Worksheet Functions

Excel Functions: Excel 2021 and 365 provide the following sorting and filtering array functions.

SORT(R1, sortcol, order): returns an array with the data in R1 in sorted order based on the elements in column sortcol (the “sort key”) of R1 (default 1). If order = 1 (default), the sort is in ascending order; if order = -1, the sort is in descending order.

UNIQUE(R1): returns an array with the unique entries in the column array R1.

FILTER(R1, criteria, if_empty): returns an array with the subset of the data in R1 that meets the criteria; if none of the data meets the criteria then the if_empty value is returned (if omitted then a #CALC! error value is returned)

SORT Function

E.g. to sort the data in Figure 1 of Sorting and Filtering and place the output in the range starting in cell F4, insert the dynamic array formula =SORT(A4:D12,4) in cell F4 and press Enter. The result is shown in Figure 1.

SORT function

Figure 1 – SORT function

UNIQUE Function

Placing the dynamic array formula =UNIQUE(D4:D12) in cell K4 returns the output shown in column K of Figure 2. Placing the dynamic array formula =SORT(UNIQUE(D4:D12)) in cell M4 returns the output shown in column M of Figure 2.

UNIQUE function

Figure 2 – UNIQUE function

FILTER Function

We can place the formula

=FILTER(A4:D12, (D4:D12>=35000)*(D4:D12<=45000),””)

in cell O4 to extract all the people from Figure 1 who have income between 35,000 and 45,000 inclusive, as shown in Figure 3.

FILTER function with AND

Figure 3 – FILTER function (AND criteria)

Note that we use “*” to specify that two criteria must both hold (i.e. as an AND of the two criteria). We can use “+” to specify that one of two criteria must hold (i.e. as an OR of the two criteria). We can use a combination of one or more “+” and “*” to specify more involved criteria.

We can place the formula

=FILTER(A4:D12,(D4:D12>40000)+(B4:B12=“F”),””)

in cell T4 to extract all the entries from Figure 1 of people who have income that exceeds 40,000 or are female, as shown in Figure 4.

FILTER function with OR

Figure 4 – FILTER function (OR criteria)

Note that the criteria expression must refer to a complete row or column or subrange of the range specified in the first argument in FILTER. Thus, while you can use =FILTER(A4:D12,D4:D12>40000) you can’t use =FILTER(A4:D12,D4:D10>40000).

SORTBY Function

Excel Function: Excel 2021 and 365 also provide the following enhanced sorting function:

SORTBY(R1, sortcol1, order1, sortcol2, order2, …): returns an array with the data in R1 in sorted order based on the elements in the column array sortcol1 (the “sort key”) of R1 (default 1). If order1 = 1 (default), the sort is in ascending order; if order1 = -1, the sort is in descending order. If specified via sortcol2 you can also use a secondary sort key (and additional sort keys  sortcol3, etc. if you like).

Whereas the sortcol argument in SORT is the index to a column in R1, in SORTBY sortcol1 is a column array or range that may or may not be part of R1, although it must have the same number of rows as R1.

E.g. if you want to sort the data in Figure 1 by income and then by the person’s name (as in Figure 5 of Sorting and Filtering), you can enter the formula =SORTBY(A4:D12,D4:D12,1,A4:A12,1) in cell Y4 and press Enter, as shown in Figure 5.

SORTBY function

Figure 5 – SORTBY function

If you want to sort first by age in ascending order and then by income in descending order but you don’t want the incomes to appear in the output, you can place the formula =SORTBY(A4:C12,C4:C12,1,D4:D12,-1) in cell AD4 and press Enter, as shown in Figure 6.

Another SORTBY example

Figure 6 – Another SORTBY example

Examples Workbook

Click here to download the Excel workbook with the examples described on this webpage.

References

Mellor, L. (2020) Sort and filter using dynamic array functions in Microsoft Excel
https://www.laramellortraining.co.uk/how-to-sort-and-filter-using-dynamic-array-functions-in-microsoft-excel

Stack Overflow (2021) Sortby function combined with Unique combined with filter functions
https://stackoverflow.com/questions/68246608/sortby-function-combined-with-unique-and-filter-functions-how-to-use-it-correcl

Proctor, M. (2020) SORTBY function in Excel. Excel off the Grid
https://exceloffthegrid.com/sortby-function-in-excel/

 

5 thoughts on “Sorting and Filtering Functions”

  1. You can remove my previous comment, I actually figured it out on my own.

    Although an example of UNIQUE and FILTER used together would enhance this page.

    Thanks…sorry for the extra work.

    Reply
    • Anita,
      The following could be an example realted to the data in D4:D12.
      The formula =UNIQUE(FILTER(D4:D12,D4:D12>40000)) outputs a column array with the values 45000 80000 60000 (the extra 45000 output by FILTER is eliminated).
      The formula =SORT(UNIQUE(FILTER(D4:D12,D4:D12>40000))) outputs the column array with values 45000 60000 80000.
      Does this do the job?
      Charles

      Reply
  2. Hi…. thanks so much for these explanations!

    I would love to see an example of UNIQUE and FILTER used together.

    I am using =UNIQUE(FILTER(A:BQ,E:E=”MKT”,” “)) which works fine within the same tab, but when I try to use this same formula on a different tab referring back to the original tab, I am getting a #SPILL! error.

    When using the fx box, it gives me the correct output as the result, clicking OK gives me the SPILL error, and reclicking the fx box puts the entire formula back in the array box, so something isn’t right, but I can’t figure out what.

    Thanks in advance!

    Reply
  3. Hello,

    Thank you for the excellent content! Have you combined the SORT (or SORTBY) with the FILTER function?

    I have good results with this FILTER formula:
    =FILTER(MasterData, ((Score>=2) + (NamedIncrease=”Type1″)) * ((FY23Values>1000) * (FY24Values>=O8)), “No Results”)

    I also get good results with this SORTBY formula:
    =SORTBY(MasterData, Score, -1)

    When I attempt to combine them, I get a VALUE error:
    =SORTBY(FILTER(MasterData, ((Score>=2) + (NamedIncrease=”Type1″)) * ((FY23Values>1000) * (FY24Values>=O8)), “No Results”), Score, -1)

    Thoughts? Do SORT (or SORTBY) anf FILTER functions work together. I’m not having luck with either order ( FILTER(SORTBY) or SORTBY(FILTER) ) using Windows 11 and Office Home and Business 2021. Thank you for your time!

    Reply
    • Hello Richard,
      Yes, sometimes these functions don’t work together. There can be many reasons for this.
      I have seen that this is true for some Real Statistics functions. When I discover this, I often need to tweak one of the functions to make it work.
      Charles

      Reply

Leave a Comment