I’m working on sorting a list of objects, and unfortunately, I’m not quite getting the information from the debugging to see where I’m going wrong.
I have a custom class that I implemented a CompareTo method in, and I call .Sort() on a List of items of that class. Unfortunately, my program never actually gets to the compareTo() method…it errors out and shutdowns down immediately on the call to the .Sort().
What generally should I be on the look out for?
Here’s my class definition, interface listing for the class.
/// <summary>
/// Summary description for ClientWorkspace.
/// </summary>
public class ClientWorkspace : IStorable
{ }
I didn’t list the compareTo method since it never even gets to that code.
I believe the exception message would be something like this: “Failed to compare two elements in the array” with an innerexception of “At least one object must implement the IComparable interface”. This gives you what you need to know:
You haven’t declared your class to implement the
IComparableinterface.It is not enough to just implement the
CompareTomethod, since the sorting algorithms will look for theIComparableinterface before attempting to callCompareTothrough that interface.…and that is why your method isn’t getting called.