Problem with delegates while sorting

K

kriz

Hi guy
In System.Collections.Generic.SortedDictionary one of the constructors takes
an IComparer<T> as its argument.I'd like to sort my dictionary by its key.Im
using
class CComparer : IComparer<person>

{

public int Compare(person p1, person p2)

{

return p1.age.CompareTo(p2.age);

}

}

then

SortedDictionary<person , char> list = new SortedDictionary<person ,
char>(new CComparer());

It works fine but i'd like to use delegates instead. Unfortunately compiler
gives me an error 'cannot convert from 'anonymous method' to
'System.Collections.Generic.IDictionary'' My delegate is mistakenly taken
for IDictionary,which is an argument in another constructor ;/, any ideas?

SortedDictionary<person, char> list = new SortedDictionary<person,
char>(delegate (person p1,person p2)

{ return p1.age.CompareTo(p2.age); });



Best regards!
 
J

Jon Skeet [C# MVP]

It works fine but i'd like to use delegates instead. Unfortunately compiler
gives me an error 'cannot convert from 'anonymous method' to
'System.Collections.Generic.IDictionary'' My delegate is mistakenly taken
for IDictionary,which is an argument in another constructor ;/, any ideas?

There aren't any constructors to SortedDictionary which take a
Comparison<T> - that's the problem.

If it's any help, I've got a ComparisonConverter class in my MiscUtil
library which implements IComparer<T> having been passed a Comparison
<T> in the constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top