Thank you. I understand the problem in my thinking now. I see that I needed to sort previous to the foreach loop rather than in it. I know there are always multiple ways to do things, but this one makes sense to me.
I learn more and more about less and less until eventually I know everything about nothing.