You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
StephenCleary edited this page May 12, 2014
·
2 revisions
Sometimes you need to define a comparer at run-time; for example, applying a user-selected multi-column sort. The comparer extension methods work on any type of comparer, so you can construct a comparer starting from the Null comparer like this:
var compareColumns = new[] { "LastName", "FirstName" };
IComparer<Person> comparer = Compare<Person>.Null();
foreach (var column in compareColumns)
{
var localColumn = column;
Func<Person, string> selector = p => p.GetType().GetProperty(localColumn).GetValue(p, null) as string;
comparer = comparer.ThenBy(selector);
}