/// <summary> /// Converts the given comparison function to a comparer object. /// </summary> /// <typeparam name="T">The type of the items that are compared.</typeparam> /// <param name="comparison">The comparison function to convert.</param> /// <returns>A new comparer that wraps the comparison function.</returns> /// <exception cref="System.ArgumentNullException">The comparison function is null.</exception> public static IComparer <T> ToComparer <T>(this Func <T, T, int> comparison) { if (comparison == null) { throw new ArgumentNullException("comparison"); } return(ComparisonWrapper <T> .GetComparer(comparison)); }
/// <summary> /// Creates a new KeyComparer that sorts using the results of the key selector. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <param name="keySelector">A function that selects a key.</param> /// <param name="keyComparison">The comparison delegate to use to compare keys.</param> /// <returns>A KeyComparer using the key selector.</returns> /// <exception cref="System.ArgumentNullException">The key selector is null.</exception> /// <exception cref="System.ArgumentNullException">The key comparison delegate is null.</exception> public static KeyComparer <T> OrderBy <TKey>(Func <T, TKey> keySelector, Func <TKey, TKey, int> keyComparison) { if (keySelector == null) { throw new ArgumentNullException("keySelector"); } if (keyComparison == null) { throw new ArgumentNullException("keyComparison"); } IComparer <TKey> keyComparer = ComparisonWrapper <TKey> .GetComparer(keyComparison); return(new TypedKeyComparer <T, TKey>(keySelector, keyComparer)); }
/// <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); }