Created
January 30, 2015 18:36
-
-
Save jayotterbein/61473ae8e6df7e15edf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class FuncEqualityComparer | |
{ | |
public static IEqualityComparer<T> Create<T>(Func<T, T, bool> func) | |
{ | |
return new FuncEqualityComparerType<T>(func); | |
} | |
public static IEqualityComparer<T> CreateEqualityComparerFor<T>(this IEnumerable<T> enumerable, Func<T, T, bool> func) | |
{ | |
return new FuncEqualityComparerType<T>(func); | |
} | |
private class FuncEqualityComparerType<T> : IEqualityComparer<T> | |
{ | |
private readonly Func<T, T, bool> _compareFunc; | |
public FuncEqualityComparerType(Func<T, T, bool> compareFunc) | |
{ | |
_compareFunc = compareFunc; | |
} | |
public bool Equals(T x, T y) | |
{ | |
return _compareFunc(x, y); | |
} | |
public int GetHashCode(T obj) | |
{ | |
return 0; | |
} | |
} | |
} | |
public static class FuncComparer | |
{ | |
public static IComparer<T> Create<T>(Func<T, T, int> compareFunc) | |
{ | |
return new FuncComparerType<T>(compareFunc); | |
} | |
public static IComparer<T> CreateComparerFor<T>(this IEnumerable<T> enumerable, Func<T, T, int> compareFunc) | |
{ | |
return new FuncComparerType<T>(compareFunc); | |
} | |
private class FuncComparerType<T> : IComparer<T> | |
{ | |
private readonly Func<T, T, int> _compareFunc; | |
public FuncComparerType(Func<T, T, int> compareFunc) | |
{ | |
_compareFunc = compareFunc; | |
} | |
public int Compare(T x, T y) | |
{ | |
return _compareFunc(x, y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment