public IComparer <T> CreateKeyedComparer <TKey>(Func <T, TKey> keySelector, bool nullsFirst) { // we want to take precedence over the new keyed comparer IComparer <T> comparer = KeyedNullPlacementComparer <T, TKey> .GetComparer(Comparer, keySelector, nullsFirst); return(UnkeyedNullPlacementComparer <T> .GetComparer(comparer, NullFilter.NullsFirst)); }
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)); }
/// <summary> /// Extends a comparer such that null values are handled up-front by treating them /// as the largest possible value. /// </summary> /// <typeparam name="T">The type of the values being compared.</typeparam> /// <typeparam name="TKey">The type of the key returned by the selector.</typeparam> /// <param name="baseComparer">The comparer to extend to handle nulls.</param> /// <param name="keySelector">Returns the key that will be inspected for null values.</param> /// <returns>A new comparer that treats nulls as the largest possible value.</returns> /// <exception cref="System.ArgumentNullException">The base comparer is null.</exception> /// <exception cref="System.ArgumentNullException">The key selector is null.</exception> /// <remarks>The returned comparer is guaranteed to never pass a null value to the given base comparer.</remarks> public static IComparer <T> NullsLast <T, TKey>(this IComparer <T> baseComparer, Func <T, TKey> keySelector) { if (baseComparer == null) { throw new ArgumentNullException("baseComparer"); } if (keySelector == null) { throw new ArgumentNullException("keySelector"); } IPrecedenceEnforcer <T> previous = baseComparer as IPrecedenceEnforcer <T>; if (previous != null) { return(previous.CreateKeyedComparer(keySelector, nullsFirst: false)); } return(KeyedNullPlacementComparer <T, TKey> .GetComparer(baseComparer, keySelector, nullsFirst : false)); }
public IComparer <T> CreateKeyedComparer <TOtherKey>(Func <T, TOtherKey> keySelector, bool nullsFirst) { // we don't know if the key selector is identical, so we must wrap ourselves return(KeyedNullPlacementComparer <T, TOtherKey> .GetComparer(this, keySelector, nullsFirst)); }