public static IOrderedEnumerable <TSource> OrderByDescending <TSource, TKey>( this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, IComparer <TKey> comparer) { if (source == null) { throw new ArgumentNullException("source"); } if (keySelector == null) { throw new ArgumentNullException("keySelector"); } comparer = new ReverseComparer <TKey>(comparer ?? Comparer <TKey> .Default); return(new OrderedEnumerable <TSource, TKey>(source, keySelector, comparer)); }
public IOrderedEnumerable <TElement> CreateOrderedEnumerable <TKey>(Func <TElement, TKey> keySelector, IComparer <TKey> comparer, bool descending) { if (keySelector == null) { throw new ArgumentNullException("keySelector"); } comparer = comparer ?? Comparer <TKey> .Default; if (descending) { comparer = new ReverseComparer <TKey>(comparer); } // Copy to a local variable so we don't need to capture "this" Func <TElement, TCompositeKey> primarySelector = compositeSelector; Func <TElement, CompositeKey <TCompositeKey, TKey> > newKeySelector = element => new CompositeKey <TCompositeKey, TKey>(primarySelector(element), keySelector(element)); IComparer <CompositeKey <TCompositeKey, TKey> > newKeyComparer = new CompositeKey <TCompositeKey, TKey> .Comparer(compositeComparer, comparer); return(new OrderedEnumerable <TElement, CompositeKey <TCompositeKey, TKey> > (source, newKeySelector, newKeyComparer)); }