/// <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);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }