Here is my class with integer and string variables
class foo
{
int age;
string name;
}
How do I create an IComparer that takes in foo as a class without the need for casting from an object?
class SortAgeAscendingHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
foo f1 = (foo)a;
foo f2 = (foo)b;
if (f1.age > f2.age)
{
return 1;
}
if (f1.age < f2.age)
{
return -1;
}
else
{
return 0;
}
}
}
public static IComparer SortAgeAscending()
{
return (IComparer)new SortAgeAscendingHelper();
}
I only wish to sort a certain index range of an array of foo's
Array.Sort(foos, 0, fooCount, SortAgeAscending());
so I can't see any other way than to use IComparer, is this really the case?