I’m using the NumericComparer code located here. It’s very easy to add it to a project: NumericComparer
I have a List of strings with numbers in them and my code is simply this: myList.Sort(new NumericComparer());
The error I am getting is this:
cannot convert from ‘ns.NumericComparer’ to ‘System.Collections.Generic.IComparer’
Any ideas why?
It looks like the
Sortmethod is expecting an implementation ofIComparer<T>— generic, with a type parameter, whereasNumericComparerimplements the non-genericIComparerinterface.So, if your list is, say a
List<decimal>, you need to supply anIComparer<decimal>.You should be able to quickly put together a class that leverages
NumericComparer:So now you can call
myList.Sort(new GenericNumericComparer<decimal>());(Note you can actually call your generic class
NumericComparertoo — it is distinguished by the type parameter. I added “Generic” here for clarity.)