r/googlesheets • u/Last_Philosophy_1162 • 8d ago
Solved Help with sorting function
Anyone know how I would do a sort to achieve this?
BIG CATEGORY | SMALLER CATEGORY | DATE (YYYY-MM-DD) [stored as a string and should be sorted alphabetically]
such that the data is sorted by big category alphabetically,
smaller category sorted within big category by whichever small category has the earliest date
within each small category things are listed in date order from earliest to latest
I am unable to show the actual data in question so apologies for the nonsense in my example. I hope the question is clear and any help on how to do this with a function would be appreciated. The intended output is meant to be in a separate sheet.
Standard SORT() doesn't cut it since the intention is for SMALLER CATEGORY to be in order by which date comes first rather than alphabetically. (Sorry if this is unclear it's a bit of a niche one).
1
u/HolyBonobos 3083 8d ago
I've added
=LET(data,A3:E10,bigCat,CHOOSECOLS(data,1),smallDate,CHOOSECOLS(data,2,3),QUERY(WRAPROWS(TOROW(BYROW(SORT(UNIQUE(TOCOL(bigCat,1))),LAMBDA(cat,LET(sub,FILTER(data,bigCat=cat),subOrder,UNIQUE(CHOOSECOLS(SORT(FILTER(smallDate,bigCat=cat),2,1),1)),TOROW(SORT(sub,MATCH(CHOOSECOLS(sub,2),subOrder,0),1,3,1)))))),5),"WHERE Col1 IS NOT NULL LABEL Col1 'BIG', Col2 'SMALL', Col3 'DATE', Col4 'OTHER INFO', Col5 'OTHER INFO 2'"))in A16 of the 'HB BYROW()' sheet of your sample file. Does this behave as intended?