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 =
                     .OrderBy(fruit => fruit.Length)
                     .ThenByDescending(fruit => fruit, new CaseInsensitiveComparer());

                 foreach (string fruit in query)

                 This code produces the following output:


LINQ ThenByDescending Query Operator  (949 Views)