Skip to content

Instantly share code, notes, and snippets.

@searope
Last active June 4, 2019 16:14
Show Gist options
  • Select an option

  • Save searope/47da119afb0a8948ada97ff2aefcedf5 to your computer and use it in GitHub Desktop.

Select an option

Save searope/47da119afb0a8948ada97ff2aefcedf5 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
public class SortComparer<T, R> : Comparer<T> where R : IComparable<R>
{
public readonly Func<T, R> GetComparableProperty;
private SortComparer(){}
private SortComparer(Func<T, R> func)
{
GetComparableProperty = func;
}
public static IComparer<T> Create(Func<T, R> func)
{
return new SortComparer<T, R>(func);
}
public override int Compare(T x, T y)
{
return GetComparableProperty(x).CompareTo(GetComparableProperty(y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment