ThenByDescending operator is prototyped and behaves just like the ThenBy operator.
It orders in descending order.
Both the ThenBy and ThenByDescending operators accept a different type of input sequence than most LINQ to Objects deferred query operators.
They take an IOrderedEnumerable<T> as the input sequence.
OrderBy or OrderByDescending operator must be called first to create an IOrderedEnumerable, then you can call the ThenBy or ThenByDescending operators.
The sort performed by the ThenBy operator is stable.
Unlike OrderBy and OrderByDescending, ThenBy and ThenByDescending are stable sorts.
This operator has two prototypes we will cover.
The First ThenByDescending Prototype
public static IOrderedEnumerable<T> ThenByDescending<T, K>( this IOrderedEnumerable<T> source, Func<T, K> keySelector) where K : IComparable<K>;
This prototype of the operator orders in descending order.
ThenByDescending has a second prototype that looks like the following:
The Second ThenBy Descending Prototype
public static IOrderedEnumerable<T> ThenByDescending<T, K>( this IOrderedEnumerable<T> source, Func<T, K> keySelector, IComparer<K> comparer); This prototype is the same as the first except it allows for a comparer object to be passed. If this version of the ThenByDescending operator is used, then it is not necessary that K implement the IComparable interface.
ArgumentNullException is thrown if any arguments are null.
The code above outputs where the names within each name length are sorted alphabetically in descending order.