Comparation 0.1.7
Requires NuGet 2.12 or higher.
dotnet add package Comparation --version 0.1.7
NuGet\Install-Package Comparation -Version 0.1.7
<PackageReference Include="Comparation" Version="0.1.7" />
<PackageVersion Include="Comparation" Version="0.1.7" />
<PackageReference Include="Comparation" />
paket add Comparation --version 0.1.7
#r "nuget: Comparation, 0.1.7"
#:package Comparation@0.1.7
#addin nuget:?package=Comparation&version=0.1.7
#tool nuget:?package=Comparation&version=0.1.7
Comparation
Comparation is tiny library for work with equality and ordering.
Installation
Install NuGet package using Package Manager
Install-Package Comparation
Equality
With Comparation you will be able to define custom equality in just few lines
using System;
using System.Collections.Generic;
using Comparation;
IEqualityComparer<Version> equality = Equality.Of<Version>()
.By(version => version.Major)
.AndBy(version => version.Minor);
equality.Equals(new Version(2, 17, 4), new Version(2, 17, 5)); // returns true
equality.Equals(new Version(2, 19, 4), new Version(2, 17, 4)); // returns false, Minor components are different
This is useful when you need to override equality in your own way or define it for a library type
that does not provide proper Equals and GetHashCode methods.
You can also pass equality into collections
var equality = Equality.Of<string>().By(@string => @string.Length);
var pets = new HashSet<string>(equality);
pets.Add("Dog");
pets.Add("Cat"); // Bad day for Cat ;), pets already contain element with length 3
pets.Add("Turtle");
string.Join(", ", pets); // returns "Dog, Turtle"
And finally you can easily compare entire collections
IEqualityComparer<IReadOnlyCollection<string>> equality = Equality.Of<string>().Collection();
var required = new[] {"engine", "body", "door", "door", "windshield"};
var inventory = new[] {"body", "door", "windshield", "engine"};
equality.Equals(required, inventory); // returns false, second door is missing
var deliveries = new[] {"body", "door", "engine", "windshield", "door"};
equality.Equals(required, deliveries); // returns true
Order
Order is defined very similar to equality
IComparer<string> order = Order.Of<string>()
.By(@string => @string[0])
.ThenBy(@string => @string.Length);
order.Compare("Apple", "Banana"); // returns -1, (Apple less than Banana) by first letter
order.Compare("Brown", "Bohr"); // returns 1, (Brown greater than Bohr) by length since first letters are same
order.Compare("Cat", "Can"); // returns 0, (Cat equal to Can) by first letter and length
Order is useful when you need to customize sorting criteria at run time.
Do you want to reverse order? Easy - use .Invert()
var ascendingOrder = Order.Of<int>().Default;
var descendingOrder = ascendingOrder.Invert();
var numbers = new List<int> {7, 9, 16, 3};
numbers.Sort(descendingOrder); // returns 16, 9, 7, 3
With order you can compare sequences like this
IComparer<IEnumerable<int>> order = Order.Of<int>().Sequence();
var myLuckyNumbers = new[] {1, 7, 32, 14, 4};
var lotteryNumbers = new[] {1, 7, 32, 28, 4};
order.Compare(myNumbers, lotteryNumbers); // returns -1, (14 is less than 28)
order.Compare(new[] {1, 2, 3}, new[] {1, 2}); // returns 1, sequences match by prefix, but first is longer
Or just get Max() value from two
var order = Order.Of<int>().Default;
order.Max(19, 7); // returns 19
order.Min(19, 7); // returns 7
You can also benefit from Sign() extension method to avoid mind-blowing work with -1, 0 and 1[^1]
var myLuckyNumbers = new[] {1, 7, 32, 14, 4};
var lotteryNumbers = new[] {1, 7, 32, 28, 4};
order.Sign(myNumbers, lotteryNumbers); // returns Sign.Less, (14 is less than 28)
order.Sign(new[] {1, 2, 3}, new[] {1, 2}); // returns Sign.Greater, sequences match by prefix, but first is longer
[^1]: Actually, negative, zero, and positive https://docs.microsoft.com/en-us/dotnet/api/system.collections.icomparer.compare
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.