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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Comparation" Version="0.1.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Comparation" Version="0.1.7" />
                    
Directory.Packages.props
<PackageReference Include="Comparation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Comparation --version 0.1.7
                    
#r "nuget: Comparation, 0.1.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Comparation@0.1.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Comparation&version=0.1.7
                    
Install as a Cake Addin
#tool nuget:?package=Comparation&version=0.1.7
                    
Install as a Cake Tool

Comparation

Comparation is tiny library for work with equality and ordering.

Comparation

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last Updated
0.1.7 178 10/1/2025
0.1.6 5,442 11/23/2021
0.1.5 1,298 11/20/2021
0.1.3 538 3/25/2021
0.1.2 511 3/22/2021
0.1.1 499 3/22/2021
0.1.0 526 3/22/2021