private static IOrderedDataProducer <TSource> OrderBy <TSource, TKey>(IDataProducer <TSource> source, DotNet20.Func <TSource, TKey> selector, IComparer <TKey> comparer, bool descending)
        {
            source.ThrowIfNull("source");
            comparer.ThrowIfNull("comparer");

            IComparer <TSource> itemComparer = new ProjectionComparer <TSource, TKey>(selector, comparer);

            if (descending)
            {
                itemComparer = itemComparer.Reverse();
            }

            // first, discard any existing "order by"s by going back to the producer
            IOrderedDataProducer <TSource> orderedProducer;
            bool first = true;

            while ((orderedProducer = source as IOrderedDataProducer <TSource>) != null)
            {
                if (first)
                {
                    // keep the top-most comparer to enforce a balanced sort
                    itemComparer = new LinkedComparer <TSource>(itemComparer, orderedProducer.Comparer);
                    first        = false;
                }
                source = orderedProducer.BaseProducer;
            }
            return(new OrderedDataProducer <TSource>(source, itemComparer));
        }
        private static IOrderedDataProducer <TSource> ThenBy <TSource, TKey>(IOrderedDataProducer <TSource> source, DotNet20.Func <TSource, TKey> selector, IComparer <TKey> comparer, bool descending)
        {
            comparer.ThrowIfNull("comparer");
            IComparer <TSource> itemComparer = new ProjectionComparer <TSource, TKey>(selector, comparer);

            if (descending)
            {
                itemComparer = itemComparer.Reverse();
            }
            itemComparer = new LinkedComparer <TSource>(source.Comparer, itemComparer);
            return(new OrderedDataProducer <TSource>(source, itemComparer));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sorts the elements in the entire System.Collections.Generic.List{T} using
        /// a projection.
        /// </summary>
        /// <param name="source">Data source</param>
        /// <param name="selector">The projection to use to obtain values for comparison</param>
        /// <param name="comparer">The comparer to use to compare projected values (on null to use the default comparer)</param>
        /// <param name="descending">Should the list be sorted ascending or descending?</param>
        public static void Sort <T, TValue>(this List <T> source, DotNet20.Func <T, TValue> selector, IComparer <TValue> comparer, bool descending)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (comparer == null)
            {
                comparer = Comparer <TValue> .Default;
            }
            IComparer <T> itemComparer = new ProjectionComparer <T, TValue>(selector, comparer);

            if (descending)
            {
                itemComparer = itemComparer.Reverse();
            }
            source.Sort(itemComparer);
        }
Ejemplo n.º 4
0
    /// <summary>
    /// Sorts the elements in the entire System.Collections.Generic.List{T} using
    /// a projection.
    /// </summary>
    /// <param name="source">Data source</param>
    /// <param name="selector">The projection to use to obtain values for comparison</param>
    /// <param name="comparer">The comparer to use to compare projected values (on null to use the default comparer)</param>
    /// <param name="descending">Should the list be sorted ascending or descending?</param>
    public static void Sort <T, TValue>(this List <T> source, Func <T, TValue> selector, IComparer <TValue> comparer, bool descending)
    {
        switch (source)
        {
        case null:
            throw new ArgumentNullException("source");
        }

        comparer = comparer switch
        {
            null => Comparer <TValue> .Default,
            _ => comparer
        };
        IComparer <T> itemComparer = new ProjectionComparer <T, TValue>(selector, comparer);

        switch (descending)
        {
        case true:
            itemComparer = itemComparer.Reverse();
            break;
        }
        source.Sort(itemComparer);
    }