Designing Sortable Collections using IComparable

.NET Framework Class Library provides several interfaces to support additional functionality in collections such as ICollection, IList, and IComparable. IComparable interface is used to support the sorting in collections.In this article, I will discuss how to use IComparable interface to design sortable arrays.

.NET Framework Class Library provides several interfaces to support additional functionality in collections such as ICollection, IList, and IComparable.

Implementing IComparable

IComparable interface is used to support the sorting in collections. It has one method - CompareTo. When you are desiging sortable objects, you must implement CompareTo method of IComparable.

For example, I create a Developer class, which is inherited from IComparable.

  1. public class   
  2. Developer : IComparable  
  3. {  
  4. }
Now I implement CompareTo method like following:

  1. public int   
  2. CompareTo(object obj)  
  3. {  
  4. // Some code   
  5. here  
  6. }
Now let's say, Developer class has a property called ID and you want your items to be sorted on ID, we simply add the following code:

 

  1. public int CompareTo(object   
  2. obj)  
  3. {  
  4.       if( !(obj is Developer) )  
  5.               
  6. throw new InvalidCastException("Not a valid Developer   
  7. object.");  
  8.       Developer   
  9. developer =   
  10. (Developer)obj;   
  11.                
  12. return this.ID.CompareTo(developer.ID);  
  13. } 

As you can see from the above code, it basically compares the current value of the object with the new value.

Now let's say, the Developer object has public properties called - FirstName, LastName, Age, and Experience and you want the sorting can be done based on any of these properties.

To do so, I defined an enumeration:

  1. public enum   
  2. SortFilter  
  3. {  
  4.         
  5. FirstName,  
  6.         
  7. LastName,  
  8.         
  9. Age,  
  10.         
  11. Experience  
  12. } 

After that, I add a static property to the Developer class of type SortFilter:

  1. private static SortFilter sortingBy =   
  2. SortFilter.FirstName;   
  3. public   
  4. static   
  5. SortFilter SortingBy  
  6. {  
  7.       get { return   
  8. sortingBy; }  
  9.       set { sortingBy =   
  10. value; }  
  11. }
Now I change the CompareTo method to the following:
  1. public int CompareTo(object   
  2. obj)  
  3. {  
  4.       if( !(obj is Developer) )  
  5.               
  6. throw new InvalidCastException("Not a valid Developer   
  7. object.");  
  8.       Developer   
  9. developer =   
  10. (Developer)obj;   
  11.       switch(Developer.SortingBy)  
  12.         
  13. {  
  14.                
  15. case SortFilter.FirstName:   
  16.                    return this.FirstName.CompareTo(developer.FirstName);  
  17.           
  18. case SortFilter.LastName:   
  19.                      
  20. return this.LastName.CompareTo(developer.LastName);  
  21.         case SortFilter.Age:   
  22.             return this.Age.CompareTo(developer.Age);  
  23.         case SortFilter.Experience:  
  24.             return this.Experience.CompareTo(developer.Experience);  
  25.         default:  
  26.                     
  27. goto case SortFilter.FirstName;  
  28.         
  29. }  
  30. } 

As you can see from the above code, I have a case statement for each filter. Now my class is ready to support sorting. All I have to do is, create an ArrayList of Developer objects, set the sorting filter, and call Sort method.

The following code creates an ArrayList of Developer objects.

  1. private ArrayList   
  2. GetList()  
  3. {  
  4.       ArrayList list   
  5. new   
  6. ArrayList();  
  7.       list.Add(new   
  8. Developer("Mahesh""Chand", 30,   
  9. "C#,ASP.NET,Windows Forms", 10)) ;   
  10.       list.Add(new   
  11. Developer("Michael""Gold", 35,   
  12. "GDI+, ASP.NET", 15)) ;   
  13.       list.Add(new   
  14. Developer("Bhasker""Das", 26, "VB.NET, Web Applications", 4)) ;   
  15.       list.Add(new   
  16. Developer("Ashish""Singhal", 24,   
  17. "ADO.NET, GDI+", 4)) ;   
  18.       list.Add(new   
  19. Developer("Neel""Beniwal", 3,   
  20. "C#,ASP.NET,Windows Forms", 0)) ;   
  21.       list.Add(new   
  22. Developer("Melanie""Talmadge", 25,   
  23. "Java", 2)) ;   
  24.       return list;  
  25. }  
The following code sets the SortingBy filter as "FirstName" and calls the Sort method of ArrayList. Now ArrayList will return sorted results.
  1. ArrayList list = GetList();  
  2.   
  3. Developer.SortingBy =   
  4. (SortFilter)Enum.Parse(typeof(SortFilter),   
  5. "FirstName");  
  6.   
  7. list.Sort();  
Sorting Order

Now you may also want to add sorting order feature  (ascending or descending) to your object. To support this, I add a SortOrder enumeartion:

  1. public enum SortOrder  
  2. {  
  3.         
  4. Ascending,  
  5.     Descending  
  6. } 

I also add SortingOrder static property to the Developer class as following:

  1. private static SortOrder   
  2. sortingOrder = SortOrder.Ascending;  
  3. public   
  4. static   
  5. SortOrder SortingOrder  
  6. {  
  7.       get { return   
  8. sortingOrder; }  
  9.       set { sortingOrder =   
  10. value; }  
  11. }  
I change the CompareTo method as following. As you can see from this method, now I am checking if SortingOrder is Ascending or Descending and based on that I am comparing the values.

  1. public int CompareTo(object   
  2. obj)  
  3. {  
  4.       if( !(obj is Developer) )  
  5.               
  6. throw new   
  7. InvalidCastException("Not a valid Developer   
  8. object.");   
  9.       Developer   
  10. developer =   
  11. (Developer)obj;   
  12.       switch(Developer.SortingBy)  
  13.         
  14. {  
  15.               
  16. case SortFilter.FirstName:   
  17.                     
  18. if (Developer.sortingOrder ==   
  19. SortOrder.Ascending)  
  20.                         
  21. return this.FirstName.CompareTo(developer.FirstName);  
  22.                     
  23. else return developer.FirstName.CompareTo(this.FirstName);  
  24.               
  25. case SortFilter.LastName:   
  26.                     
  27. if (Developer.sortingOrder ==   
  28. SortOrder.Ascending)  
  29.                           
  30. return this.LastName.CompareTo(developer.LastName);  
  31.                     
  32. else return developer.LastName.CompareTo(this.LastName);  
  33.               
  34. case SortFilter.Age:   
  35.                     
  36. if (Developer.sortingOrder ==   
  37. SortOrder.Ascending)  
  38.                           
  39. return this.Age.CompareTo(developer.Age);  
  40.                     
  41. else return developer.Age.CompareTo(this.Age);  
  42.               
  43. case SortFilter.Experience:  
  44.                     
  45. if (Developer.sortingOrder ==   
  46. SortOrder.Ascending)  
  47.                           
  48. return this.Experience.CompareTo(developer.Experience);  
  49.                     
  50. else return developer.Experience.CompareTo(this.Experience);  
  51.               
  52. default:  
  53.                     
  54. goto case SortFilter.FirstName;  
  55.         
  56. }  
  57. }  
Now only thing I have to do is, set the SortingOrder property of Developer object and call Sort method. That should do it. See the attached source code for more details.

 

  1. Developer.SortingOrder =   
  2. (SortOrder)Enum.Parse(typeof(SortOrder), "Descending");  
  3.   
  4. list.Sort();  
Summary

In this article, I discussed how to use IComparable interface to design a collection of sortable objects.