Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick IComparer?

Tags:

c#

.net

icomparer

Before I go reinventing the wheel, is there some framework way of creating an IComparer<T> from a Func<T,T,int>?

EDIT

IIRC (it's been a while) Java supports anonymous interface implementations. Does such a construct exist in C#, or are delegates considered a complete alternative?

like image 677
spender Avatar asked Apr 25 '11 18:04

spender


1 Answers

In the upcoming .NET4.5 (Visual Studio 2012) this is possible with the static factory method Comparer<>.Create. For example

IComparer<Person> comp = Comparer<Person>.Create(
    (p1, p2) => p1.Age.CompareTo(p2.Age)
    );
like image 188
Jeppe Stig Nielsen Avatar answered Oct 20 '22 03:10

Jeppe Stig Nielsen