-
-
Notifications
You must be signed in to change notification settings - Fork 33
Dynamic Comparers
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);
}
Equality comparers also support a Null
comparer.
You can specify dynamic
as the compared type, which allows you to define a comparer that can be applied to multiple, unrelated types:
var comparer = Compare<dynamic>.OrderByDescending(x => x.Priority);
The comparer defined above can be used with any type that has a Priority
property.