Skip to content

Commit

Permalink
Added generic version of Comparer
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Jul 26, 2019
1 parent aba7234 commit a1e5710
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Comparer[T].cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;

namespace Platform.Comparers
{
public class Comparer<T> : IComparer<T>
{
private readonly Func<T, T, int> _compare;

public Comparer(IComparer<T> comparer)
: this(comparer.Compare)
{
}

public Comparer(Func<T, T, int> compare) => _compare = compare;

public int Compare(T x, T y) => _compare(x, y);

public static implicit operator Comparer<T>(Func<T, T, int> compare) => new Comparer<T>(compare);

public static implicit operator Func<T, T, int>(Comparer<T> comparer) => comparer._compare;
}
}

0 comments on commit a1e5710

Please sign in to comment.