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).
Title
of null? Or maybe there is a null in your list? – Wamsleychildren
. – Parental