public void TestThenByComparerPreserved() { var sequence = new[] { new { A = "2", B = "0" }, new { A = "1", B = "5" }, new { A = "2", B = "2" }, new { A = "1", B = "3" }, new { A = "1", B = "4" }, new { A = "2", B = "1" }, }; var comparer = new ComparerFunc <string>((a, b) => int.Parse(a).CompareTo(int.Parse(b))); var resultA1 = sequence.OrderBy(x => x.A, comparer, OrderByDirection.Ascending) .ThenBy(y => y.B, comparer, OrderByDirection.Ascending); var resultA2 = sequence.OrderBy(x => x.A, comparer) .ThenBy(y => y.B, comparer); // ensure both produce the same order Assert.IsTrue(resultA1.SequenceEqual(resultA2)); var resultB1 = sequence.OrderBy(x => x.A, comparer, OrderByDirection.Ascending) .ThenBy(y => y.B, comparer, OrderByDirection.Descending); var resultB2 = sequence.OrderBy(x => x.A, comparer) .ThenByDescending(y => y.B, comparer); // ensure both produce the same order Assert.IsTrue(resultB1.SequenceEqual(resultB2)); }
public void TestRankCustomComparer() { const int count = 10; var ordinals = Enumerable.Range(1, count); var sequence = ordinals.Select(x => new DateTime(2010, x, 20 - x)); // invert the CompareTo operation to Rank in reverse order (ascening to descending) var resultA = sequence.AsTestingSequence().Rank(ComparerFunc <DateTime> .As((a, b) => - a.CompareTo(b))); var resultB = sequence.AsTestingSequence().RankBy(x => x.Day, ComparerFunc <int> .As((a, b) => - a.CompareTo(b))); Assert.IsTrue(resultA.SequenceEqual(ordinals)); Assert.IsTrue(resultB.SequenceEqual(ordinals.Reverse())); }
public void TestOrderByComparerPreserved() { var sequence = Enumerable.Range(1, 100); var sequenceAscending = sequence.Select(x => x.ToString()); var sequenceDescending = sequenceAscending.Reverse(); var comparer = new ComparerFunc <string>((a, b) => int.Parse(a).CompareTo(int.Parse(b))); var resultAsc1 = sequenceAscending.OrderBy(x => x, comparer, OrderByDirection.Descending); var resultAsc2 = sequenceAscending.OrderByDescending(x => x, comparer); // ensure both order by operations produce identical results Assert.IsTrue(resultAsc1.SequenceEqual(resultAsc2)); // ensure comparer was applied in the order by evaluation Assert.IsTrue(resultAsc1.SequenceEqual(sequenceDescending)); var resultDes1 = sequenceDescending.OrderBy(x => x, comparer, OrderByDirection.Ascending); var resultDes2 = sequenceDescending.OrderBy(x => x, comparer); // ensure both order by operations produce identical results Assert.IsTrue(resultDes1.SequenceEqual(resultDes2)); // ensure comparer was applied in the order by evaluation Assert.IsTrue(resultDes1.SequenceEqual(sequenceAscending)); }