I assume this is a .NET version before .NET 4.0; after .NET 4.0 it is IComparable<in T>
, and should work OK in many cases - but this requires the variance changes in 4.0
The list is List<Child>
- so sorting it will try to use either IComparable<Child>
or IComparable
- but neither of those is implemented. You could implement IComparable
at the Parent
level, perhaps:
public abstract class Parent : IComparable<Parent>, IComparable {
public string Title;
public Parent(string title){this.Title = title;}
int IComparable.CompareTo(object other) {
return CompareTo((Parent)other);
}
public int CompareTo(Parent other){
return this.Title.CompareTo(other.Title);
}
}
which will apply the same logic via object
.