Consider a simple case of sorting an array of strings in C#.
We can simply call the Sort() method to sort the array.
Listing 1
ArrayList carArray = new ArrayList();
carArray.Add("Corvette");
carArray.Add("Honda");
carArray.Add("BMW");
carArray.Sort();
If you observe the contents of the array after the Sort(),
you will notice that the elements are sorted alphabetically ie.,
"BMW," "Corvette," "Honda."
However, consider a Car Class as shown in Listing 2.
Listing 2
class Car
{
public string Make { set; get; }
public int Year { set; get; }
public string Location { set; get; }
}
If you create an ArrayList of car objects and try to Sort()
it, it would throw an exception. You will need to have the Car class implement
the IComparable interface and define the CompareTo method to be able to sort
custom objects.