public void CanCreateCanonicalComparer() { var comparer = ComparerFactory <Product> .Create( x => new { x.Name.Length, x.Price }, (builder, x, y) => { builder .LessThen(() => x.Length < y.Length || x.Price < y.Price) .Equal(() => x.Length == y.Length || x.Price == y.Price) .GreaterThan(() => x.Length > y.Length || x.Price > y.Price); }); var products = new[] { new Product { Name = "Car", Price = 7 }, new Product { Name = "Table", Price = 3 }, new Product { Name = "Orange", Price = 1 }, }; var sorted = products.OrderByDescending(p => p, comparer).ToList(); Assert.AreEqual(3, sorted.Count); Assert.AreEqual("Orange", sorted.ElementAt(0).Name); // because added first Assert.AreEqual("Car", sorted.ElementAt(1).Name); // because added first Assert.AreEqual("Table", sorted.ElementAt(2).Name); // because ItemCount }
/// <summary> /// Sorts the elements of a sequence in ascending order by using a specified comparison delegate. /// </summary> public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TKey, TKey, int> comparison) { var comparer = ComparerFactory <TKey> .Create(comparison); return(source.OrderBy(keySelector, comparer)); }
public void ComparerFromFactory() { var values = new int[] { 3, 5, 2, 4, 2, 1 }; var comparer = ComparerFactory.Create <int>((x, y) => y <= x); CollectionAssert.AreEqual(new[] { 5, 4, 3, 2, 2, 1 }, values.OrderBy(x => x, comparer).ToArray()); }
static void Main(string[] args) { var list = new List <int>(); list.Sort(new ComparerDelegate <int>((x, y) => x - y)); //using without factory list.Sort(ComparerFactory.Create <int>((x, y) => x - y)); //using with factory }
public void CreateComparerFromExpression() { var comparer = ComparerFactory <Person> .Create(x => x.Age); comparer.Compare(new Person { Age = 1 }, new Person { Age = 1 }) .Should().Be(0); }
public void CanCompareSelectedValue() { var comparer = ComparerFactory <User> .Create(x => x.Age); Assert.That.Comparer().IsCanonical ( value: new User(20), less: new User(15), equal: new User(20), greater: new User(30), comparer ); Assert.IsTrue(comparer.Compare(default, new User(0)) < 0);
public void CanCreateComparer1() { var comparer = ComparerFactory <Product> .Create( x => x.Price, (builder, x, y) => { builder .LessThen(() => x < y) .Equal(() => x == y) .GreaterThan(() => x > y); }); var products = new[] { new Product { Name = "Car", Price = 7 }, new Product { Name = "Table", Price = 3 }, new Product { Name = "Orange", Price = 1 }, }; Assert.That.Comparer().IsCanonical ( new Product { Price = 3 }, new Product { Price = 2 }, new Product { Price = 3 }, new Product { Price = 4 }, comparer ); var sorted = products.OrderByDescending(p => p, comparer).ToList(); Assert.AreEqual("Car", sorted.ElementAt(0).Name); Assert.AreEqual("Table", sorted.ElementAt(1).Name); Assert.AreEqual("Orange", sorted.ElementAt(2).Name); }
public static void SortLists() { var employees = new List <Employee>(); var employeeByIdComparer = new EmployeeByIdComparer(); Comparison <Employee> comparison = (x, y) => x.Id.CompareTo(x.Id); employees.Sort(employeeByIdComparer); employees.Sort(comparison); var set = new SortedSet <Employee>(employeeByIdComparer); var comparer = ComparerFactory.Create(comparison); var set1 = new SortedSet <Employee>(comparer); }
ICollectionLoaded <TService, TCollectionType> .Sort(Func <TCollectionType, TCollectionType, int> comparerFunc) { return(((ICollectionLoaded <TService, TCollectionType>) this).Sort(ComparerFactory <TCollectionType> .Create(comparerFunc))); }
ICollectionLoaded <TService, TCollectionType> .Sort(Expression <Func <TCollectionType, IComparable> > func) { return(((ICollectionLoaded <TService, TCollectionType>) this).Sort(ComparerFactory <TCollectionType> .Create(func))); }