Reputation: 1831
I'm trying to sort a list of objects using List.Sort(
), but at runtime it tells me that it cannot compare elements in the array.
Failed to compare two elements in the array
Class structure:
public abstract class Parent : IComparable<Parent> {
public string Title;
public Parent(string title){this.Title = title;}
public int CompareTo(Parent other){
return this.Title.CompareTo(other.Title);
}
}
public class Child : Parent {
public Child(string title):base(title){}
}
List<Child> children = GetChildren();
children.Sort(); //Fails with "Failed to compare two elements in the array."
Why can I not compare subclasses of a base that implements IComparable<T>
? I'm probably missing something, but I cannot see why this should not be allowed.
Edit: Should clarify that I'm targeting .NET 3.5 (SharePoint 2010)
Edit2: .NET 3.5 is the problem (see answer below).
Upvotes: 9
Views: 902
Reputation: 1063609
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
.
Upvotes: 11