LINQ ThenByDescending Query Operator

By Peter Bromberg

Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.

public class CaseInsensitiveComparer : IComparer<string>
            {
                 public int Compare(string x, string y)
                 {
                     return string.Compare(x, y, true);
                 }
             }

             public static void ThenByDescendingEx1()
             {
                 string[] fruits =
                { "apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE" };

                 // Sort the strings first ascending by their length and
                 // then descending using a custom case insensitive comparer.
               IEnumerable<string> query =
                     fruits.AsQueryable()
                     .OrderBy(fruit => fruit.Length)
                     .ThenByDescending(fruit => fruit, new CaseInsensitiveComparer());

                 foreach (string fruit in query)
                   Console.WriteLine(fruit);
             }

             /*
                 This code produces the following output:

                 apPLe
                 apple
                 APple
                 apPLE
                 orange
                 ORANGE
                 baNanA
                 BAnana
             */



LINQ ThenByDescending Query Operator  (775 Views)