An open API service indexing awesome lists of open source software.

https://github.com/devizer/universe.yetanothercomparerbuilder

Emit-free, fast, strongly typed, highly-customizable yet another ComparerBuilder
https://github.com/devizer/universe.yetanothercomparerbuilder

Last synced: 3 months ago
JSON representation

Emit-free, fast, strongly typed, highly-customizable yet another ComparerBuilder

Awesome Lists containing this project

README

          

# Universe.YetAnotherComparerBuilder
Emit-free, fast, strongly typed, highly-customizable yet another ComparerBuilder

## The icing on the
Extended order direction can be applyed to both an element comparer and a field comparer:
```csharp
[Flags]
public enum OrderFlavour
{
Forward = 0,
Backward = 1,
NullGoesFinally = 0,
NullGoesEarly = 2,
Default = Forward | NullGoesFinally,
}
```

# Sample
In this sample all Doctors aged 42 will ahead of another persons.
```csharp
public class Demo
{
class Person
{
public string Title;
public string Name;
public int? Age;
}

public static void Test()
{
StringComparer ignoreCase = StringComparer.InvariantCultureIgnoreCase;
string highlightTitle = "Dr.";
int highlightAge = 42;

IComparer comparer = new ComparerBuilder()
.Compare(x => ignoreCase.Equals(highlightTitle, x.Title))
.Compare(x => x.Age.HasValue && highlightAge == x.Age.Value)
.CompareString(x => x.Name, ignoreCase)
.CompareString(x => x.Title, ignoreCase)
.Compare(x => x.Age, OrderFlavour.Backward | OrderFlavour.NullGoesFinally)
.GetComparer();

Enumerable.Empty().ToList().Sort(comparer);
Enumerable.Empty().OrderBy(x => x, comparer).ToList();
}
}
```