public void TestThenBy() { var sequence = new[] { new Point(1, 2), new Point(1, 1), new Point(-1, 2), new Point(-1, 1) }; sequence.OrderBy(t => t, Comparers.Create((Point p) => p.X).Reverse().ThenBy(Comparers.Create((Point p) => p.Y))) .SequenceShouldEqual(sequence.OrderByDescending(t => t.X).ThenBy(t => t.Y)); }
public void TestCreate() { var allEqual = Comparers.Create((int?i) => 0); allEqual.Compare(1, 5).ShouldEqual(0); Math.Sign(allEqual.Compare(null, 5)).ShouldEqual(-1); Math.Sign(allEqual.Compare(5, null)).ShouldEqual(1); var evenNumbersFirst = Comparers.Create((int i) => i % 2); Enumerable.Range(0, 10).OrderBy(i => i, evenNumbersFirst) .SequenceShouldEqual(new[] { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 }); }
public void TestComparerEquality() { Func <int, int> f = i => - i; TestEquality(Comparers.Create(f), Comparers.Create(f), Comparers.Create((int i) => i.ToString())); TestEquality(Comparers.Reverse <int>(), Comparers.Reverse <int>(), Comparers.Create((int i) => i.ToString()).Reverse()); var first = Comparers.Create((string s) => s.Length); var second = Comparers.Create((string s) => s[0]); TestEquality(first.ThenBy(second), first.ThenBy(second), second.ThenBy(first)); TestEquality(Comparers.GetSequenceComparer <string>(), Comparers.GetSequenceComparer(Comparer <string> .Default), Comparers.GetSequenceComparer(StringComparer.OrdinalIgnoreCase)); }
private static bool SortBasedEquals <T>(IEnumerable <T> a, IEnumerable <T> b, IEqualityComparer <T> comparer) { var order = Comparers.Create((T item) => comparer.GetHashCode(item)); return(a.OrderBy(x => x, order).SequenceEqual(b.OrderBy(x => x, order), comparer)); }