public void TestNullComparer_BuildComparer()
        {
            Random random = new Random();

            // build a list of random dates
            var dates = new List <DateTime>(getRandomDates(random));

            // randomly choose which order to sort the components by
            string[] propertyNames = { "Year", "Month", "Day", "Hour", "Minute", "Second" };
            randomShuffle(propertyNames, random);

            // build a comparer based on the order of the components
            IComparer <DateTime> dateComparer = NullComparer <DateTime> .Default;

            foreach (string propertyName in propertyNames)
            {
                string current = propertyName; // avoids non-local lambda problem
                Func <DateTime, object> getter = (DateTime d) => typeof(DateTime).GetProperty(current).GetValue(d, null);
                bool ascending = random.Next() % 2 == 0;
                if (ascending)
                {
                    dateComparer = dateComparer.ThenBy(getter);
                }
                else
                {
                    dateComparer = dateComparer.ThenByDescending(getter);
                }
            }

            // now we can sort the dates accordingly
            dates.Sort(dateComparer);
        }
示例#2
0
        /// <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="comparerFactory">The definition of the key comparer. May not be <c>null</c>.</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"/>. This value is ignored if <typeparamref name="T"/> is a non-nullable type.</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, Func <ComparerBuilderFor <TKey>, IComparer <TKey> > comparerFactory, bool specialNullHandling = false, bool descending = false)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <TKey>());

            return(source.ThenBy(selector, comparer, specialNullHandling, descending));
        }
示例#3
0
        public void ThenByWithProjection()
        {
            var data = SampleType.SampleData;
            IComparer <SampleType> primary = ProjectionComparer <SampleType> .Create(t => t.First);

            data.Sort(primary.ThenBy(t => t.Second));

            Assert.IsTrue(new[] { 2, 10, 5 }.SequenceEqual(data.Select(x => x.Second)));
        }
示例#4
0
        public void ThenByIsAppliedAsTieBreaker()
        {
            IComparer <Person> thenByComparer  = ComparerBuilder.For <Person>().OrderBy(p => p.FirstName);
            IComparer <Person> defaultComparer = ComparerBuilder.For <Person>().Default();
            IComparer <Person> fullComparer    = defaultComparer.ThenBy(thenByComparer);

            Assert.True(defaultComparer.Compare(AbeAbrams, WilliamAbrams) == 0);
            Assert.True(thenByComparer.Compare(AbeAbrams, WilliamAbrams) < 0);
            Assert.True(fullComparer.Compare(AbeAbrams, WilliamAbrams) < 0);
        }
示例#5
0
        private IComparer <DataRow> InsertOrderByType(IComparer <DataRow> orderBy)
        {
            if (orderBy == null || orderBy == OrderBy)
            {
                return(orderBy);
            }

            IComparer <DataRow> result = DataRow.OrderBy(_.Type, SortDirection.Ascending);

            return(result.ThenBy(orderBy));
        }
示例#6
0
        public void ThenByIsAppliedAsTieBreaker()
        {
            IComparer <Person> thenByComparer = Compare <string> .Default().SelectFrom((Person p) => p.FirstName);

            IComparer <Person> defaultComparer = Compare <Person> .Default();

            IComparer <Person> fullComparer = defaultComparer.ThenBy(thenByComparer);

            Assert.IsTrue(defaultComparer.Compare(AbeAbrams, WilliamAbrams) == 0);
            Assert.IsTrue(thenByComparer.Compare(AbeAbrams, WilliamAbrams) < 0);
            Assert.IsTrue(fullComparer.Compare(AbeAbrams, WilliamAbrams) < 0);
        }
示例#7
0
        public void SubstitutesCompareDefaultForNull()
        {
            IComparer <Person> thenByComparer = ComparerBuilder.For <Person>().OrderBy(p => p.FirstName);
            IComparer <Person> source         = null;
            var comparer = source.ThenBy(thenByComparer);

            Assert.Equal(ComparerBuilder.For <Person>().Default().ThenBy(thenByComparer).ToString(), comparer.ToString());

            var list = new List <Person> {
                AbeAbrams, WilliamAbrams, CaseyJohnson, JackAbrams
            };

            list.Sort(comparer);
            Assert.Equal(new[] { AbeAbrams, JackAbrams, WilliamAbrams, CaseyJohnson }, list);
        }
示例#8
0
        public void SubstitutesCompareDefaultForNull()
        {
            IComparer <Person> thenByComparer = Compare <string> .Default().SelectFrom((Person p) => p.FirstName);

            IComparer <Person> source = null;
            var comparer = source.ThenBy(thenByComparer);

            Assert.AreSame(Compare <Person> .Default(), (comparer as CompoundComparer <Person>).Source);

            var list = new List <Person> {
                AbeAbrams, WilliamAbrams, CaseyJohnson, JackAbrams
            };

            list.Sort(comparer);
            CollectionAssert.AreEqual(new[] { AbeAbrams, JackAbrams, WilliamAbrams, CaseyJohnson }, list);
        }
示例#9
0
        /// <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"/>. This value is ignored if <typeparamref name="T"/> is a non-nullable type.</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)
        {
            var selectComparer = new SelectComparer <T, TKey>(selector, keyComparer, specialNullHandling);

            return(source.ThenBy(selectComparer, descending));
        }
示例#10
0
 protected override IComparer <UserOperation> GetUniqueComparer(IComparer <UserOperation> comparer) =>
 comparer.ThenBy(CompareUserOperationsByHash.Instance);
示例#11
0
 protected override IComparer <MevBundle> GetGroupComparer(IComparer <MevBundle> comparer) //compares two bundles with same block #
 => comparer.ThenBy(CompareMevBundleByHash.Default);
示例#12
0
 public static IComparer <T> Prefer <T>(this IComparer <T> first, Predicate <T> test) =>
 first.ThenBy(new TestComparer <T>(test));
示例#13
0
 protected override IComparer <MevBundle> GetUniqueComparer(IComparer <MevBundle> comparer) //compares all the bundles to evict the worst one
 => comparer.ThenBy(CompareMevBundleByHash.Default);
 public static IComparer <Transaction> GetPoolUniqueTxComparer(this IComparer <Transaction> comparer)
 => comparer
 .ThenBy(ByHashTxComparer.Instance);         // in order to sort properly and not lose transactions we need to differentiate on their identity which provided comparer might not be doing
示例#15
0
        /// <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="comparerFactory">The definition of the key comparer. May not be <c>null</c>.</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"/>. This value is ignored if <typeparamref name="T"/> is a non-nullable type.</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, Func <ComparerBuilderFor <TKey>, IComparer <TKey> > comparerFactory, bool specialNullHandling = false, bool descending = false)
        {
            var comparer = comparerFactory(ComparerBuilder.For <TKey>());

            return(source.ThenBy(selector, comparer, specialNullHandling, descending));
        }
示例#16
0
 /// <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));
 }
示例#17
0
 internal static IComparer <Transaction> GetPoolUniqueTxComparer(IComparer <Transaction> comparer)
 => comparer
 .ThenBy(DistinctCompareTx.Instance);         // in order to sort properly and not loose transactions we need to differentiate on their identity which provided comparer might not be doing