public IComparer <T> CreateCompoundComparer(IComparer <T> comparer)
        {
            CompoundComparer <T> compoundComparer = new CompoundComparer <T>();

            compoundComparer.AppendComparison(Comparer);
            compoundComparer.AppendComparison(comparer);
            return(KeyedNullPlacementComparer <T, TKey> .GetComparer(compoundComparer.Normalize(), _keySelector, NullFilter.NullsFirst));
        }
        public static IComparer <T> GetComparer(IComparer <T> baseComparer, IComparer <T> nextComparer)
        {
            var comparer = new CompoundComparer <T>();

            comparer.AppendComparison(baseComparer);
            comparer.AppendComparison(nextComparer);
            return(comparer.Normalize());
        }
예제 #3
0
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the key comparison.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="keySelector">The key of the type to use for comparison.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The key selector is null.</exception>
        public static IComparer <T> ThenBy <T, TKey>(this IComparer <T> baseComparer, Func <T, TKey> keySelector)
        {
            if (baseComparer is null)
            {
                throw new ArgumentNullException(nameof(baseComparer));
            }
            var comparer = KeyComparer <T> .OrderBy(keySelector);

            var compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, comparer);

            return(compoundComparer);
        }
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the key comparison.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="keySelector">The key of the type to use for comparison.</param>
        /// <param name="keyComparer">The comparer to use to compare the keys.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The key selector is null.</exception>
        /// <exception cref="System.ArgumentNullException">The key comparison delegate is null.</exception>
        public static IComparer <T> ThenBy <T, TKey>(this IComparer <T> baseComparer, Func <T, TKey> keySelector, IComparer <TKey> keyComparer)
        {
            if (baseComparer == null)
            {
                throw new ArgumentNullException("baseComparer");
            }
            IComparer <T> comparer = KeyComparer <T> .OrderBy(keySelector, keyComparer);

            IComparer <T> compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, comparer);

            return(compoundComparer);
        }
예제 #5
0
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the comparer.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="comparer">The comparer to use if two items compare as equal using the base comparer.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The comparer is null.</exception>
        public static IComparer <T> ThenBy <T>(this IComparer <T> baseComparer, IComparer <T> comparer)
        {
            if (baseComparer is null)
            {
                throw new ArgumentNullException(nameof(baseComparer));
            }
            if (comparer is null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }
            var compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, comparer);

            return(compoundComparer);
        }
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the comparer.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="comparer">The comparer to use if two items compare as equal using the base comparer.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The comparer is null.</exception>
        public static IComparer <T> ThenBy <T>(this IComparer <T> baseComparer, IComparer <T> comparer)
        {
            if (baseComparer == null)
            {
                throw new ArgumentNullException("baseComparer");
            }
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }
            IComparer <T> compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, comparer);

            return(compoundComparer);
        }
        public void AppendComparison(IComparer <T> comparer)
        {
            if (comparer is NullComparer <T> )
            {
                return;
            }
            CompoundComparer <T> other = comparer as CompoundComparer <T>;

            if (other != null)
            {
                _comparers.AddRange(other._comparers);
                return;
            }
            _comparers.Add(comparer);
        }
        public static IComparer <T> GetComparer(IComparer <T> baseComparer, IComparer <T> nextComparer)
        {
            // make sure null comparer stays highest precedence
            IPrecedenceEnforcer <T> nullComparer = baseComparer as IPrecedenceEnforcer <T>;

            if (nullComparer != null)
            {
                return(nullComparer.CreateCompoundComparer(nextComparer));
            }
            CompoundComparer <T> comparer = new CompoundComparer <T>();

            comparer.AppendComparison(baseComparer);
            comparer.AppendComparison(nextComparer);
            return(comparer.Normalize());
        }
예제 #9
0
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the key comparison, in descending order.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="keySelector">The key of the type to use for comparison.</param>
        /// <param name="keyComparison">The comparison delegate to use to compare the keys.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The key selector is null.</exception>
        /// <exception cref="System.ArgumentNullException">The key comparison delegate is null.</exception>
        public static IComparer <T> ThenByDescending <T, TKey>(
            this IComparer <T> baseComparer,
            Func <T, TKey> keySelector,
            Func <TKey, TKey, int> keyComparison)
        {
            if (baseComparer == null)
            {
                throw new ArgumentNullException(nameof(baseComparer));
            }
            var comparer = KeyComparer <T> .OrderByDescending(keySelector, keyComparison);

            var compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, comparer);

            return(compoundComparer);
        }
예제 #10
0
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the comparison.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="comparison">The comparison to use if two items compare as equal using the base comparer.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The comparison delegate is null.</exception>
        public static IComparer <T> ThenBy <T>(this IComparer <T> baseComparer, Func <T, T, int> comparison)
        {
            if (baseComparer is null)
            {
                throw new ArgumentNullException(nameof(baseComparer));
            }
            if (comparison is null)
            {
                throw new ArgumentNullException(nameof(comparison));
            }
            var wrapper = ComparisonWrapper <T> .GetComparer(comparison);

            var compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, wrapper);

            return(compoundComparer);
        }
        /// <summary>
        /// Composes a comparer that performs subsequent ordering using the comparison.
        /// </summary>
        /// <typeparam name="T">The type being compared.</typeparam>
        /// <param name="baseComparer">The comparer to extend.</param>
        /// <param name="comparison">The comparison to use if two items compare as equal using the base comparer.</param>
        /// <returns>A comparer that performs comparisons using both comparison operations.</returns>
        /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception>
        /// <exception cref="System.ArgumentNullException">The comparison delegate is null.</exception>
        public static IComparer <T> ThenBy <T>(this IComparer <T> baseComparer, Func <T, T, int> comparison)
        {
            if (baseComparer == null)
            {
                throw new ArgumentNullException("baseComparer");
            }
            if (comparison == null)
            {
                throw new ArgumentNullException("comparison");
            }
            IComparer <T> wrapper = ComparisonWrapper <T> .GetComparer(comparison);

            IComparer <T> compoundComparer = CompoundComparer <T> .GetComparer(baseComparer, wrapper);

            return(compoundComparer);
        }