SORTBY Function not working as expected

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to use the FILTER Function along with the SORTBY function to sort by two different columns. However, it's not sorting the same way it does when sorting manually in a Table.
Here's what the data looks like when sorting by Title first and then by Test Date within a Table:
We can see that the Titles are in ascending order, and then the Test Dates. Titles with no Test Date remain in ascending order below the ones with a Test Date.

1715042687340.png


Here's what it looks like when I use SORTBY Title then Test Date using the Filter Function:
This method seems to disregard the 2nd Sort Order to the Test Date

1715042761828.png


Here's what my SAMPLE DATA Looks like:

VBA Testing.xlsm
ABCDEFGHIJKLM
1QuizTitleBook LevelWord CountTest DateBook StatusBook No.On HandTest DateQuizTitle
2122822Mysterious Island (Saddleback)3.56,1466-Mayavailableavailable 51361130,000 Stitches: Story of the National 9/11 Flag
3188059Who Hatches the Egg? All About Eggs3.59356-Mayavailablelibrary9-May122820A Tale of Two Cities (Saddleback)
4191363Exploring Pluto and Other Dwarf Planets3.84637-Mayavailableon hold193924Apollo's First Moon Landing
5195765When God Made Light3.65387-Mayavailable8-May7172Berenstain Bears to the Rescue
67172Berenstain Bears to the Rescue3.75828-Mayavailable122187Best of Poe (Saddleback)
7501222United States Navy3.72508-Mayavailable9-May52030Box Turtle at Silver Pond Lane
8122820A Tale of Two Cities (Saddleback)3.65,1719-Mayavailable59784Corduroy's Christmas Surprise
952030Box Turtle at Silver Pond Lane3.88739-Mayavailable59785Corduroy's Easter Party
10191357Exploring Comets and Asteroids3.941010-Mayavailable10-May191357Exploring Comets and Asteroids
11509378Lara Croft: Tomb Raider Hero3.934210-Mayavailable7-May191363Exploring Pluto and Other Dwarf Planets
1251361130,000 Stitches: Story of the National 9/11 Flag3.9596on hold180821From Tadpole to Frog
13193924Apollo's First Moon Landing3.81,941available141878God Gave Us the World
14122187Best of Poe (Saddleback)3.95,212available500157Harriet Tubman: Freedom Fighter
1559784Corduroy's Christmas Surprise3.3993available122803Hunchback of Notre Dame (Saddleback)
1659785Corduroy's Easter Party3.0857available5519Jumanji
17180821From Tadpole to Frog2.5240available10-May509378Lara Croft: Tomb Raider Hero
18141878God Gave Us the World2.6870available122805Last of the Mohicans (Saddleback)
19500157Harriet Tubman: Freedom Fighter3.8667on hold192177Lion and the Mouse
20122803Hunchback of Notre Dame (Saddleback)3.65,479available122806Man in the Iron Mask (Saddleback)
21105146It's the Easter Beagle, Charlie Brown3.51,527on order59949Mount Rushmore
225519Jumanji3.91,800on hold6-May122822Mysterious Island (Saddleback)
23122805Last of the Mohicans (Saddleback)3.56,112available186903Paul Revere's Ride
24192177Lion and the Mouse3.3600available186904Pilgrims' Voyage to America
25122806Man in the Iron Mask (Saddleback)3.25,674available122824Red Badge of Courage (Saddleback)
2659949Mount Rushmore3.7636available111465Soccer
2736459New Kid at School3.310,043on order116860Town Mouse and the Country Mouse
28186903Paul Revere's Ride3.81,791available8-May501222United States Navy
29186904Pilgrims' Voyage to America3.71,918available122829War of the Worlds (Saddleback)
30122824Red Badge of Courage (Saddleback)2.73,758available111746Welcome to Narnia
31111465Soccer3.3444available7-May195765When God Made Light
3216860Town Mouse and the Country Mouse3.91,059available6-May188059Who Hatches the Egg? All About Eggs
33122829War of the Worlds (Saddleback)3.83,740available64293Zathura: A Space Adventure
34111746Welcome to Narnia3.3467available
3564293Zathura: A Space Adventure3.82,273on hold
SortBy
Cell Formulas
RangeFormula
K2:M33K2=LET( x,FILTER(CHOOSECOLS(t_Students,5,1,2),COUNTIF(l_OnHand,t_Students[Book Status])), SORTBY(x,INDEX(x,,3),1,INDEX(x,,1),1))
Dynamic array formulas.
Named Ranges
NameRefers ToCells
l_OnHand=t_BookStatus[On Hand]K2:M33

Any help would be greatly appreciated.
Thank you…
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Your data in the first picture are NOT sorted by Title. The SORTBY is doing it correctly.
If you use Auto-filter on data, you can only sort one column at a time - in your case the sort order is by Test Date as indicated by the arrow on the dropdown indicator.
If it were sorted by Title the 'A' titles would come before the 'B' titles and so on, as they are in the SORTBY result.

If you want to sort by more than one field then you need to use Sort from the ribbon, not autofilter sorting, like so:

1715056455568.png


1715056513481.png


If you do this you will get the same result as the SORTBY. When you sort by 2 or more fields, the second and subsequent fields are only used as tiebreakers. Thus in your case the date only becomes relevant to the sort if you have two identical titles.
 
Upvote 0
Try:
Excel Formula:
=CHOOSECOLS(SORTBY(A2:E35,E2:E35,1,B2:B35,1),1,2,5)
 
Upvote 0
Another option
Excel Formula:
=LET(x,FILTER(CHOOSECOLS(t_Students,5,1,2),COUNTIF(l_OnHand,t_Students[Book Status])),SORT(x,{1,3}))
 
Upvote 0
Solution
Another option
Excel Formula:
=LET(x,FILTER(CHOOSECOLS(t_Students,5,1,2),COUNTIF(l_OnHand,t_Students[Book Status])),SORT(x,{1,3}))
Hello Fluff,

Thanks for your help on this! I was able to plug your formula in 'as is' and it produced the filtered/sorted data perfectly to match what I was doing with the Table data.
VBA Testing.xlsm
ABCDEFGHIJKLM
1SAMPLE DATAFLUFF
2QuizTitleBook LevelWord CountTest DateBook StatusBook No.On HandTest DateQuizTitle
3122822Mysterious Island (Saddleback)3.56,1466-Mayavailableavailable6-May122822Mysterious Island (Saddleback)
4188059Who Hatches the Egg? All About Eggs3.59356-Mayavailablelibrary6-May188059Who Hatches the Egg? All About Eggs
5191363Exploring Pluto and Other Dwarf Planets3.84637-Mayavailableon hold7-May191363Exploring Pluto and Other Dwarf Planets
6195765When God Made Light3.65387-Mayavailable7-May195765When God Made Light
77172Berenstain Bears to the Rescue3.75828-Mayavailable8-May7172Berenstain Bears to the Rescue
8501222United States Navy3.72508-Mayavailable8-May501222United States Navy
9122820A Tale of Two Cities (Saddleback)3.65,1719-Mayavailable9-May122820A Tale of Two Cities (Saddleback)
1052030Box Turtle at Silver Pond Lane3.88739-Mayavailable9-May52030Box Turtle at Silver Pond Lane
11191357Exploring Comets and Asteroids3.941010-Mayavailable10-May191357Exploring Comets and Asteroids
12509378Lara Croft: Tomb Raider Hero3.934210-Mayavailable10-May509378Lara Croft: Tomb Raider Hero
1351361130,000 Stitches: Story of the National 9/11 Flag3.9596on hold51361130,000 Stitches: Story of the National 9/11 Flag
14193924Apollo's First Moon Landing3.81,941available193924Apollo's First Moon Landing
15122187Best of Poe (Saddleback)3.95,212available122187Best of Poe (Saddleback)
1659784Corduroy's Christmas Surprise3.3993home59785Corduroy's Easter Party
1759785Corduroy's Easter Party3.0857available180821From Tadpole to Frog
18180821From Tadpole to Frog2.5240available141878God Gave Us the World
19141878God Gave Us the World2.6870available500157Harriet Tubman: Freedom Fighter
20500157Harriet Tubman: Freedom Fighter3.8667on hold5519Jumanji
21122803Hunchback of Notre Dame (Saddleback)3.65,479home122805Last of the Mohicans (Saddleback)
22105146It's the Easter Beagle, Charlie Brown3.51,527on order192177Lion and the Mouse
235519Jumanji3.91,800on hold122806Man in the Iron Mask (Saddleback)
24122805Last of the Mohicans (Saddleback)3.56,112available59949Mount Rushmore
25192177Lion and the Mouse3.3600available186904Pilgrims' Voyage to America
26122806Man in the Iron Mask (Saddleback)3.25,674available122824Red Badge of Courage (Saddleback)
2759949Mount Rushmore3.7636available111465Soccer
2836459New Kid at School3.310,043on order116860Town Mouse and the Country Mouse
29186903Paul Revere's Ride3.81,791home111746Welcome to Narnia
30186904Pilgrims' Voyage to America3.71,918available64293Zathura: A Space Adventure
31122824Red Badge of Courage (Saddleback)2.73,758available
32111465Soccer3.3444available
3316860Town Mouse and the Country Mouse3.91,059available
34122829War of the Worlds (Saddleback)3.83,740home
35111746Welcome to Narnia3.3467available
3664293Zathura: A Space Adventure3.82,273on hold
37
SortBy
Cell Formulas
RangeFormula
K3:M30K3=LET(x,FILTER(CHOOSECOLS(t_Students,5,1,2),COUNTIF(l_OnHand,t_Students[Book Status])),SORT(x,{1,3}))
Dynamic array formulas.
Named Ranges
NameRefers ToCells
l_OnHand=t_OnHand[On Hand]K3:M30

I didn't realize you could use more than one criteria with the SORT function in that way (as an array?): SORT(x,{1,3})
I saw examples of using INDEX and it seemed to make sense to me… but wasn't sure why it failed to produce the results I was expecting.

I have another one I'm working on where the Word Count needs to be sorted DESCENDING and Test Date ASCENDING (default), so I tested it using your method: SORT(x,{1,3},{-1,1}). I ran my test by reversing the dates and keeping the titles in order:
VBA Testing.xlsm
KLM
2Test DateQuizTitle
310-May191357Exploring Comets and Asteroids
410-May509378Lara Croft: Tomb Raider Hero
59-May122820A Tale of Two Cities (Saddleback)
69-May52030Box Turtle at Silver Pond Lane
78-May7172Berenstain Bears to the Rescue
88-May501222United States Navy
97-May191363Exploring Pluto and Other Dwarf Planets
107-May195765When God Made Light
116-May141878God Gave Us the World
126-May122806Man in the Iron Mask (Saddleback)
136-May122822Mysterious Island (Saddleback)
146-May186904Pilgrims' Voyage to America
156-May188059Who Hatches the Egg? All About Eggs
1651361130,000 Stitches: Story of the National 9/11 Flag
17193924Apollo's First Moon Landing
18122187Best of Poe (Saddleback)
SortBy
Cell Formulas
RangeFormula
K3:M30K3=LET(x,FILTER(CHOOSECOLS(t_Students,5,1,2),COUNTIF(l_OnHand,t_Students[Book Status])),SORT(x,{1,3},{-1,1}))
Dynamic array formulas.
Named Ranges
NameRefers ToCells
l_OnHand=t_OnHand[On Hand]K3:M18

This worksperfectly and is a great improvement to all of my earlier failed experiments…

Thanks again, Fluff for all your help.
 
Upvote 0
Try:
Excel Formula:
=CHOOSECOLS(SORTBY(A2:E35,E2:E35,1,B2:B35,1),1,2,5)
Hi Cubist,

Thanks for your suggestion. Although it's missing the FILTER function, it certainly sorts the data correctly.
I also had to limit my filtering to my Named Range (l_OnHand) and the Column Order for the output would be (5,1,2); no big deal there as it's a simple fix.
I'm trying to use the FILTER Function along with the SORTBY function to sort by two different columns.
Thanks again; I appreciate you taking the time to offer me help. (y)
 
Upvote 0
Your data in the first picture are NOT sorted by Title.
Hi myall_blues,

I was manaully sorting the Table by Title first then by Date. Excel only shows the last column sorted... which may imply I was only sorting by Date. The sample data shows that each date has the titles in ascending order, and Titles without dates are allso in ascending order...

Currently I'm using a macro to build out my print log in a Table, but I'm trying to switch over to a single formula using Filtering/Sorting.. which will later be activated by a macro to choose betweento types of Print Logs...

Thanks again and best regards...
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top