I have the following unsorted list:
List<string> myUnsortedList = New List<string>();
myUnsortedList.Add("Alpha");
myUnsortedList.Add("(avg) Alpha");
myUnsortedList.Add("Zeta");
myUnsortedList.Add("Beta");
myUnsortedList.Add("(avg) Beta");
myUnsortedList.Add("(avg) Zeta");
I want to sort the list descending alphabetical order, then have the value with (avg) right after the normal value:
Final Result: Zeta, (avg) Zeta, Beta, (avg) Beta, Alpha, (avg) Alpha
My application is written in C# and I want to use LINQ to accomplish the sorting
This should work ok for what you need, assuming "(avg)" is the only special prefix
This will order all the stings descending not including the "(avg) " then it will order by the strings length this way the string with the "(avg)" prefix will come after the one without
var result = myUnsortedList.OrderByDescending(x => x.Replace("(avg) ", "")).ThenBy(x => x.Length);
Final Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With