示例#1
0
        public static IOrderedEnumerable <TSource> ThenByDescending <TSource, TKey>(this IOrderedEnumerable <TSource> source, Func <TSource, TKey> keySelector, IComparer <TKey> comparer)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (keySelector == null)
            {
                throw new ArgumentNullException("keySelector");
            }

            IComparer <TSource> sourceComparer = new ProjectionComparer <TSource, TKey>(keySelector, comparer);

            sourceComparer = new ReverseComparer <TSource>(sourceComparer);

            return(new OrderedEnumerable <TSource>(source, sourceComparer));
        }
示例#2
0
        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));
        }