public void SubstitutesCompareDefaultForNull() { IComparer <int> source = null; var comparer = source.SelectFrom((Person p) => p.Priority); Assert.AreSame(Compare <int> .Default(), (comparer as SelectComparer <Person, int>).Source); var list = GetPeople(); list.Sort(comparer); CollectionAssert.AreEqual(new[] { 2, 3, 4, 5 }, list.Select(x => x.Priority).ToList()); }
/// <summary> /// Returns a comparer that uses a key comparer if the source comparer determines the objects are equal. /// </summary> /// <typeparam name="T">The type of objects being compared.</typeparam> /// <typeparam name="TKey">The type of key objects being compared.</typeparam> /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param> /// <param name="selector">The key selector. May not be <c>null</c>.</param> /// <param name="keyComparer">The key comparer. Defaults to <c>null</c>. If this is <c>null</c>, the default comparer is used.</param> /// <param name="allowNulls">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>.</param> /// <param name="descending">A value indicating whether the sorting is done in descending order. If <c>false</c> (the default), then the sort is in ascending order.</param> /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns> public static IFullComparer <T> ThenBy <T, TKey>(this IComparer <T> source, Func <T, TKey> selector, IComparer <TKey> keyComparer = null, bool allowNulls = false, bool descending = false) { Contract.Requires(selector != null); Contract.Ensures(Contract.Result <IFullComparer <T> >() != null); return(source.ThenBy(keyComparer.SelectFrom(selector, allowNulls), descending)); }
/// <summary> /// Returns a comparer that uses a key comparer if the source comparer determines the objects are equal. /// </summary> /// <typeparam name="T">The type of objects being compared.</typeparam> /// <typeparam name="TKey">The type of key objects being compared.</typeparam> /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param> /// <param name="selector">The key selector. May not be <c>null</c>.</param> /// <param name="keyComparer">The key comparer. Defaults to <c>null</c>. If this is <c>null</c>, the default comparer is used.</param> /// <param name="specialNullHandling">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>.</param> /// <param name="descending">A value indicating whether the sorting is done in descending order. If <c>false</c> (the default), then the sort is in ascending order.</param> /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns> public static IFullComparer <T> ThenBy <T, TKey>(this IComparer <T> source, Func <T, TKey> selector, IComparer <TKey> keyComparer = null, bool specialNullHandling = false, bool descending = false) { return(source.ThenBy(keyComparer.SelectFrom(selector, specialNullHandling), descending)); }