Excel: sorting an array on 3 different columns using functions
Posted by jpluimers on 2025/09/09
Given Excel is the most popular functional language (see the video under my post “Everybody should have an obsession with Lisp-like language at least once in their life” @KevlinHenney), this post is on sorting with Excel functions.
I had an array in Excel starting at row 2 (to exclude the headings) where I had to sort on 3 different columns: C, A, B (or numerically 3, 1, 2) so [Wayback/Archive] sorting – How to multi level sort an array in excel, using formulas? I am aware about the way, using SORT button on DATA tab – Stack Overflow (thanks [Wayback/Archive] Armaan Gohil and [Wayback/Archive] Jos Woolley) didn’t fully apply but did put me on the right track as I wasn’t aware that the SORT function allows to specify multiple columns using an embedded array argument.
The functions that worked:
=SORTBY(A2:.D1048576,C2:.C1048576,1,A2:.A1048576,1,B2:.B1048576,1)=SORT(A2#,{3,2,1})=SORT(A2:.D1048576,{3,1,2})
Notes:
1048576is the maximum number of rows in Excel 2007 and up; for Excel 2003 and lower you would use65536- the
:.is a trick to skip empty values at the end of the array, see the belowTRIMRANGEfunction reference - the
#is a trick named spilled range operation to skip empty cells at the end of an array as well {3,1,2}is 1 row by 3 column array constant with values3,1,2; ordered arrays like{1,2,3}and{3,2,1}could be replaced bySEQUENCE(,3)and=SEQUENCE(,3,3,-1)which can be easier localising with varieties in settings for separators;(US-English: row separator) and,(US-English: column separator)- [Wayback/Archive] SORT function – Microsoft Support does not indicate the second parameter can be an array:
=SORT(array,[sort_index],[sort_order],[by_col])
Argument Description array
RequiredThe range, or array to sort [sort_index]
OptionalA number indicating the row or column to sort by [sort_order]
OptionalA number indicating the desired sort order; 1 for ascending order (default), -1 for descending order [by_col]
OptionalA logical value indicating the desired sort direction; FALSE to sort by row (default), TRUE to sort by column
In the end, I wanted to convert the array to a list and trim it down to unique and non-empty values. A query for the former suggested to use ARRAYTOTEXT, but that results in text, not a list. You can kind of combine it with TEXTSPLIT if the ARRAY values themselves do not contain the split delimiter.
FILTER seems an OK option to filter empty cells, though “empty” has various manifestations in Excel: empty string "", zero integer 0, no value at all, error, and others.
Be aware of the #VALUE! result when using FILTER: though that whereas the filter range is a 2-dimensional array, the filter criteria are 1-dimensional arrays that need to be either as wide or as tall as the filter range (see one of the links below)
Also in that link: for array functions, there is a little &"" trick that converts any empty array cell into a cell having an empty string "".
Without including owners, I could have chosen for a jagged or sparse array/table containing the instruments per opus, then go from there using for instance HSTACK/STACK/FILTER.
In the end, I used these functions:
=FILTER('Percussie-per-werk'!D2:.F1048576,('Percussie-per-werk'!C2:.C1048576<>"niemand")*('Percussie-per-werk'!C2:.C1048576<>""))which gets me the percussion instruments that somebody (note the*trick: not"niemand"*and* not"") plays as we have limited percussionists=UNIQUE(A2:.C1048576)gets the unique list=SORT(E2#,{3,2,1})sorts the list on columns3(is this on the program),2(owner),1(instrument)
and
=FILTER('Percussie-per-werk'!A2:.D1048576,('Percussie-per-werk'!C2:.C1048576<>"niemand")*('Percussie-per-werk'!C2:.C1048576<>""))gets the opus (column A), part layout(column B), who (column C) and instrument (column D)=UNIQUE(A2#)just in case there are any duplicates=SORT(F2#,{3,4,1})shows who (column 4) has what (column 3) to play in which opus (column 1)=UNIQUE(A2:.A1048576)has the set list
Various instruments are owned by the club, various by individuals, and there are overlaps, hence getting a complete list is important.
| Dutch | English |
|---|---|
| Percussie-per-werk | Percussion-by-opus |
| Instrumenten-en-eigenaren | Instruments-and-owners |
| Wiens | Whose |
| Niemand | Nobody |
| Partij | Part layout |
In the future, I want to experiment with the LET function as pointed to in [Wayback/Archive] All values of an array to one column or row | Microsoft Community Hub (thanks [Wayback/Archive] SergeiBaklan)
If don’t touch functions which are only in Beta now, mock-up could be=LET( data, $B2:Q350, rs, ROWS(data), cls, COLUMNS(data), k, SEQUENCE(rs*cls), v, INDEX(data, MOD(k-1,rs)+1, INT((k-1)/rs)+1 ), UNIQUE( FILTER(v, v<>"") ) )
Similarly, in the figure I might use CHOOSE, CHOOSECOLS (if my data was row-based, that would be CHOOSEROWS) to select sets of data to combine into an array for further processing.
References:
- [Wayback/Archive] Use array constants in array formulas – Microsoft Support
- [Wayback/Archive] Name an array constant – Microsoft Support
- [Wayback/Archive] Guidelines and examples of array formulas – Microsoft Support
- [Wayback/Archive] Spilled range operator – Microsoft Support
- [Wayback/Archive] Dynamic array formulas and spilled array behavior – Microsoft Support
- [Wayback/Archive]
LAMBDAfunction – Microsoft Support - [Wayback/Archive]
MAPfunction – Microsoft Support - [Wayback/Archive]
REDUCEfunction – Microsoft Support - [Wayback/Archive]
INDIRECTfunction – Microsoft Support - [Wayback/Archive] Guidelines and examples of array formulas – Microsoft Support
- [Wayback/Archive]
SEQUENCEfunction – Microsoft Support - [Wayback/Archive]
SORTfunction – Microsoft Support - [Wayback/Archive]
SORTBYfunction – Microsoft Support - [Wayback/Archive]
UNIQUEfunction – Microsoft Support - [Wayback/Archive]
TRIMRANGEfunction – Microsoft Support - [Wayback/Archive]
ARRAYTOTEXTfunction – Microsoft Support - [Wayback/Archive]
TEXTSPLITfunction – Microsoft Support - [Wayback/Archive]
FILTERfunction – Microsoft Support - [Wayback/Archive]
HSTACKfunction – Microsoft Support - [Wayback/Archive]
VSTACKfunction – Microsoft Support - [Wayback/Archive]
AGGREGATEfunction – Microsoft Support (AVERAGE, COUNT, COUNTA, MAX, MIN, PRODUCT, STDEV.S, STDEV.P, SUM, VAR.S, VAR.P, MEDIAN, MODE.SNGL, LARGE, SMALL, PERCENTILE.INC, QUARTILE.INC, PERCENTILE.EXC, QUARTILE.EXC – list generated using=ARRAYTOTEXT(B1:B19)on the table inside the AGGREGATE article) - [Wayback/Archive]
EXPANDFunction – Microsoft Support - [Wayback/Archive]
CHOOSEfunction – Microsoft Support - [Wayback/Archive]
CHOOSECOLSfunction – Microsoft Support - [Wayback/Archive]
CHOOSEROWSfunction – Microsoft Support - [Wayback/Archive]
TAKEfunction – Microsoft Support - [Wayback/Archive]
TOCOLfunction – Microsoft Support - [Wayback/Archive]
TOROWfunction – Microsoft Support - [Wayback/Archive] Look up values with
VLOOKUP,INDEX, orMATCH– Microsoft Support- [Wayback/Archive]
INDEXfunction – Microsoft Support - [Wayback/Archive]
MATCHfunction – Microsoft Support - [Wayback/Archive]
XMATCHfunction – Microsoft Support - [Wayback/Archive]
HLOOKUPfunction – Microsoft Support - [Wayback/Archive]
VLOOKUPfunction – Microsoft Support - [Wayback/Archive]
XLOOKUPfunction – Microsoft Support
- [Wayback/Archive]
- [Wayback/Archive]
- [Wayback/Archive] Dynamic array formulas vs. legacy CSE array formulas – Microsoft Support
- [Wayback/Archive] Implicit intersection operator:
@– Microsoft Support - [Wayback/Archive] Lookup and reference functions (reference) – Microsoft Support
- [Wayback/Archive]
LETfunction – Microsoft Support
Posts/questions that inspired me:
- [Wayback/Archive] Combine ranges and arrays in Excel:
VSTACK&HSTACKfunctions - [Wayback/Archive] Text split to array – Excel formula | Exceljet
- [Wayback/Archive] worksheet function – How to do this very specific concatenation and transform an array to list using Excel? – Super User (thanks [Wayback/Archive] Jos Woolley, [Wayback/Archive] Prasanna and [Wayback/Archive] HighConceptTroglodyte)
- [Wayback/Archive] What is the behavior of Excel with jagged dynamic arrays ? : excel (thanks [Wayback/Archive] solexNY-LI and [Wayback/Archive] PaulieThePolarBear): I especially like the summaries given by bot-user [Wayback/Archive] Decronym showing the overview of various Excel functions used in a Reddit thread.
- [Wayback/Archive] PaulieThePolarBear comments on Excel: Filter & Combine about the
LETfunction - [Wayback/Archive] Excel – Array Formula to skip empty cells – Stack Overflow (thanks [Wayback/Archive] PhoenixJay, [Wayback/Archive] barry houdini and [Wayback/Archive] Kevin Holliday)
If your data starts in A1, use this formula:=FILTER(A1:A8,A1:A8<>"")In the first argument of the FILTER function, one identifies the array of data to return.In the second argument, one identifies the criteria by which to filter. In this case, the requester wants to skip blanks, so the condition is not equal to (<>) null (“”).Ensure the return and filter ranges use the same rows. In this case, rows 1 through 8. But you can use other columns as necessary.Other answers include options usingIFERROR,COUNTAand comparisons - [Wayback/Archive] How to extract a list of data in a row ignoring blanks (Excel 2013) – Super User (thanks [Wayback/Archive] Solarinoos, [Wayback/Archive] Ron Rosenfeld and [Wayback/Archive] Máté Juhász)
If you use latest version of Excel, you can use FILTER function:
=FILTER(A1:G1,A1:G1<>"")or for earlier versions, fiddle with
INDEX,AGGREGATE,COUNTA,COLUMN,ROWor even addIFERROR,COUNT,COLUM,COLUMS– there you see the newer functions remove so much complexity - [Wayback/Archive] microsoft excel – #VALUE! Error when trying to use the Filter function to filter on mutiple criteria – Super User (thanks [Wayback/Archive] novawaly and [Wayback/Archive] Scott Craner):
Q
this is my formula:
=FILTER(S1:Y5, ISNUMBER(SEARCH("match_name", $S$1:$Y$1)) * ($S$1:S$5=A2) * (IF(S1:Y5<>"", 1, 0)))however, no matter what I do – i keep getting an error
A
While the return can be a 2D array, all the checks must be a 1D array and all must be the same size and shape and also must have either the same number of rows or the same number of columns as the return array.
So in you case you will want to do nested FILTERS:
=FILTER(FILTER(S1:Y5,ISNUMBER(SEARCH("match_name",S1:Y1))),S1:S5="AGI Partners, LLC")&""Now it first filters to the correct columns then it filters that result to the correct row.
C
The
&""turns the blanks to blanks otherwise they return0.C
With it returning a 2d array Unique will not do a line by line or column by column removal. It will look at the whole row or whole column of the entire array for duplicates not cell by cell. If just want a unique list (one Column) then use
TOCOLon the 2D array and wrap that inUNIQUE. - [WaybackSave/Archive] All of Excel’s Dynamic Array Functions
- a
Queries:
- [Wayback/Archive] excel sort multiple columns at DuckDuckGo
- [Wayback/Archive] excel array formula sort multiple columns at DuckDuckGo
- [Wayback/Archive] excel function convert array to list at DuckDuckGo
- [Wayback/Archive] excel array skip empty rows at DuckDuckGo
- [Wayback/Archive] excel vstack at DuckDuckGo
- [Wayback/Archive] excel function text to array at DuckDuckGo
- [Wayback/Archive] excel function jagged array to list at DuckDuckGo
- [Wayback/Archive] excel function array to list at DuckDuckGo
- [Wayback/Archive] excel function concatenate arrays at DuckDuckGo
- [Wayback/Archive] excel function split on comma to array at DuckDuckGo
--jeroen
Rate this:
Share this:
- Click to share on Mastodon (Opens in new window) Mastodon
- Click to share on Bluesky (Opens in new window) Bluesky
- Share on Tumblr
- Click to share on Reddit (Opens in new window) Reddit
- Click to share on Threads (Opens in new window) Threads
- Tweet
- Click to share on Telegram (Opens in new window) Telegram
- Click to share on Nextdoor (Opens in new window) Nextdoor
- Click to share on WhatsApp (Opens in new window) WhatsApp
- Click to print (Opens in new window) Print
- Click to email a link to a friend (Opens in new window) Email
Related
This entry was posted on 2025/09/09 at 18:00 and is filed under Development, Excel, Functional Programming, Office, Power User, Reddit, SocialMedia, Software Development. Tagged: VALUE. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
This site uses Akismet to reduce spam. Learn how your comment data is processed.






Leave a comment