---------------------------------------------------------------------
Consider a simple case of sorting an array of strings in C#. We can simply call the Sort() method to sort the array.
Code Snippet
- carArray.Add("Corvette");
- carArray.Add("Honda");
- carArray.Add("BMW");
- carArray.Sort();
End of Code Snippet
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.
Code Snippet
- class Car
- {
- public string Make { set; get; }
- public int Year { set; get; }
- public string Location { set; get; }
- }
End of Code Snippet
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.
Method 1: Implementing the IComparable Interface
Step 1: Implement the IComparable interface
Code Snippet
- class Car : IComparable
End of Code Snippet
Step 2: Define the CompareTo method.
In this example we will be sorting by the Make property of the Car Class.
Code Snippet
- public int CompareTo(object obj)
- {
- {
- Car c2 = (Car)obj;
- return Make.CompareTo(c2.Make);
- }
- else
- }
End of Code Snippet
That is it! We are now ready to test if our sorting works.
Step 3: Test Sorting by Make.
Code Snippet
- objCar.Make = "BMW";
- objCar.Year = 2008;
- objCar.Location = "Florida";
- carArray.Add(objCar);
- objCar = null;
- objCar.Make = "Honda";
- objCar.Year = 1996;
- objCar.Location = "Illinois";
- carArray.Add(objCar);
- objCar = null;
- objCar.Make = "Corvette";
- objCar.Year = 2006;
- objCar.Location = "California";
- carArray.Add(objCar);
- objCar = null;
- carArray.Sort();
End of Code Snippet
You will now observe that the carArray is sorted alphabetically by Make.
Note: Sometimes instead of using the ArrayList, it is possible that you are working with an object array. You can use the C# built in Adapter() method as shown below.
To convert from an array to ArrayList use:
Code Snippet
- ArrayList carArray = ArrayList.Adapter(carObjectArray);
End of Code Snippet
To convert from an ArrayList to object array use:
Method 2: Using the IComparer Interface
Sometimes, it might be necessary to have more flexibility in your sorting, for example, to provide which property you want to sort the Car array by. In situations like this, we would need to use the IComparer interface and use the overloaded Sort method that takes the comparer instance as an argument.
Step 1: Create a CarComparer class that implements the IComparer interface
Code Snippet
- class CarComparer: IComparer
- {
- public enum ComparisonType
- {
- Make = 1, Year, Location
- }
- public ComparisonType ComparisonMethod
- {
- set;
- get;
- }
- public int Compare(object x, object y)
- {
- Car c1;
- Car c2;
- c1 = x as Car;
- else
- c2 = y as Car;
- else
- return c1.CompareTo(c2, ComparisonMethod);
- }
- }
End of Code Snippet
The main purpose of the CarComparer is to keep track of by what property we are sorting. It has a ComparisonType enum that has the property elements and an overloaded Compare method.
Step 2
Add an overloaded CompareTo to the Car class as shown in Listing 7.
Code Snippet
- public int CompareTo(Car c2, CarComparer.ComparisonType comparisonType)
- {
- switch (comparisonType)
- {
- case CarComparer.ComparisonType.Make:
- return Make.CompareTo(c2.Make);
- case CarComparer.ComparisonType.Year:
- return Year.CompareTo(c2.Year);
- case CarComparer.ComparisonType.Location:
- return Location.CompareTo(c2.Location);
- default:
- return Make.CompareTo(c2.Make);
- }
- }
End of Code Snippet
That is it, we are done. We now have added the ability to sort by Make, Year or Location. Let us test it out.
Step 3
Code Snippet
- objCar.Make = "BMW";
- objCar.Year = 2008;
- objCar.Location = "Florida";
- carArray.Add(objCar);
- objCar = null;
- objCar.Make = "Honda";
- objCar.Year = 1996;
- objCar.Location = "Illinois";
- carArray.Add(objCar);
- objCar = null;
- objCar.Make = "Corvette";
- objCar.Year = 2006;
- objCar.Location = "ZZ";
- carArray.Add(objCar);
- objCar = null;
- carComparer.ComparisonMethod = CarComparer.ComparisonType.Location;
- carArray.Sort(carComparer);
End of Code Snippet
We use the overloaded Sort method that takes an instance of the CarComparer class. To sort by other properties, change the ComparisonMethod as shown below.
Code Snippet
- carComparer.ComparisonMethod = CarComparer.ComparisonType.Year
End of Code Snippet
Conclusion
This article demonstrates different ways of sorting an array of custom objects. It provides a step-by-step approach in using the IComparable interface and in creating a Comparer class for sorting.
1 comment:
Very useful, thanks!
Post a Comment