Problem with delegates while sorting

  • Thread starter Thread starter kriz
  • Start date Start date
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!
 
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.
 
Back
Top