Today I am going to explain how to sort the ArrayList of objects using two properties; IComparable and IComparator Interface.
Before implementing it using Comparable and Comparator Interface, let's see what happens if we use "Collections.Sort()" like we use for normal ArrayList
Let us create an Employee class which has "empId","empName","empAge" as properties in it. Now I want ArrayList of Employee object type.
Code for Employee Class,
- public class Employee
- {
- private int empId;
- private string empName;
- private int empAge;
-
- public Employee(int empId, string empName, int empAge)
- {
- this.empId = empId;
- this.empName = empName;
- this.empAge = empAge;
- }
-
- public string GetEmployeeName()
- {
- return empName;
- }
-
- public void SetEmployeeName(string empName)
- {
- this.empName = empName;
- }
-
- public int GetEmployeeID()
- {
- return empId;
- }
-
- public void SetEmployeeID(int empId)
- {
- this.empId = empId;
- }
-
- public int GetEmployeeAge()
- {
- return empAge;
- }
- public void SetEmployeeAge(int empAge)
- {
- this.empAge = empAge;
- }
-
- }
Now let us look in to the main method, and we shall sort it using "Collections.Sort()"
- public static void Main(string[] args)
- {
- var employeeList = new ArrayList();
- employeeList.Add(new Employee(10, "Test1", 26));
- employeeList.Add(new Employee(20, "Test2", 36));
- employeeList.Add(new Employee(30, "Test3", 46));
-
- employeeList.Sort();
- foreach(var emp in employeeList)
- {
- Console.WriteLine(emp);
- }
-
- }
Now let us see what output it gives.
We encountered an Exception. Look at the exception -- it says: "System.Invalid.Operation". That means Sort() method is only used to sort normal array list with "integers" or "string" types. But for sorting the complex types, it will not know which type to sort (Ex: EmpAge or EmpSalary). So we get an error, "invalid operation".
Let us see what IComparable Interface is.
- We use IComparable Interface to sort elements. It is used to compare the current instance with another object of the same type. It is of type integer.
- It gives us "compareTo()" to implement, while comparing two objects of similar type.
Syntax
public class ClassName : IComparable<ClassName>
You need to implement method inside the "IComparable" interface. Please find the below code snippet
- public class Employee : IComparable<Employee>
- {
- private int empId;
- private string empName;
- private int empAge;
-
- public Employee(int empId, string empName, int empAge)
- {
- this.empId = empId;
- this.empName = empName;
- this.empAge = empAge;
- }
-
- public string GetEmployeeName()
- {
- return empName;
- }
-
- public void SetEmployeeName(string empName)
- {
- this.empName = empName;
- }
-
- public int GetEmployeeID()
- {
- return empId;
- }
-
- public void SetEmployeeID(int empId)
- {
- this.empId = empId;
- }
-
- public int GetEmployeeAge()
- {
- return empAge;
- }
- public void SetEmployeeAge(int empAge)
- {
- this.empAge = empAge;
- }
-
- public int CompareTo(Employee obj)
- {
- return empAge.CompareTo(obj.empAge);
-
- }
-
- }
In the above class we have implemented the method "compareTo()" based on EmpAge. Now let us look at the Main method and o/p
-
- List<Employee> listOfemployees = new List<Employee>();
- listOfemployees.Add(new Employee(100, "John", 30));
- listOfemployees.Add(new Employee(600, "Kate", 42));
- listOfemployees.Add(new Employee(30, "William", 41));
- listOfemployees.Add(new Employee(50, "George", 10));
- listOfemployees.Add(new Employee(500, "Charlotte", 50));
-
- listOfemployees.Sort();
- foreach(var emp in listOfemployees)
- {
- Console.WriteLine(emp.GetEmployeeAge());
- }
ICompareble Interface can sort the elements only one at a time, i.e it can only sort one field at a time. Like I have mentioned in the above example, I have sorted using age field
Let us see IComparer Interface.
- Used to sort the element that compares two objects
- It has Compare() method to implement
Now let us look at the example and see how we can implement Employee class using IComparer Interface:
- public class Employee
- {
- private int empId;
- private string empName;
- private int empAge;
-
- public Employee(int empId, string empName, int empAge)
- {
- this.empId = empId;
- this.empName = empName;
- this.empAge = empAge;
- }
-
- public string GetEmployeeName()
- {
- return empName;
- }
-
- public void SetEmployeeName(string empName)
- {
- this.empName = empName;
- }
-
- public int GetEmployeeID()
- {
- return empId;
- }
-
- public void SetEmployeeID(int empId)
- {
- this.empId = empId;
- }
-
- public int GetEmployeeAge()
- {
- return empAge;
- }
- public void SetEmployeeAge(int empAge)
- {
- this.empAge = empAge;
- }
- }
- public class EmployeeComparer : IComparer<Employee>
- {
-
- public enum sortBy
- {
- empId,
- empName,
- empAge
- }
-
-
- public sortBy compareByFields = sortBy.empAge;
- public int Compare(Employee x, Employee y)
- {
- switch (compareByFields)
- {
- case sortBy.empAge:
- return x.GetEmployeeAge().CompareTo(y.GetEmployeeAge());
- case sortBy.empName:
- return x.GetEmployeeName().CompareTo(y.GetEmployeeName());
- case sortBy.empId:
- return x.GetEmployeeID().CompareTo(y.GetEmployeeID());
- default:break;
-
- }
- return x.GetEmployeeAge().CompareTo(y.GetEmployeeAge());
-
- }
- }
Let us see the main method and output:
- List<Employee> listOfemployees = new List<Employee>();
- listOfemployees.Add(new Employee(100, "John", 30));
- listOfemployees.Add(new Employee(600, "Kate", 42));
- listOfemployees.Add(new Employee(30, "William", 41));
- listOfemployees.Add(new Employee(50, "George", 10));
- listOfemployees.Add(new Employee(500, "Charlotte", 50));
-
-
- EmployeeComparer compare = new EmployeeComparer();
- compare.compareByFields = EmployeeComparer.sortBy.empName;
-
- listOfemployees.Sort(compare);
- foreach (var emp in listOfemployees)
- {
- Console.WriteLine(emp.GetEmployeeName());
- }
- Console.WriteLine("************Sort By Age**************");
-
- compare.compareByFields = EmployeeComparer.sortBy.empAge;
-
- listOfemployees.Sort(compare);
- foreach (var emp in listOfemployees)
- {
- Console.WriteLine(emp.GetEmployeeName() + " " + emp.GetEmployeeAge());
- }
- Console.WriteLine("************Sort By EmployeeID**************");
- compare.compareByFields = EmployeeComparer.sortBy.empId;
-
- listOfemployees.Sort(compare);
- foreach (var emp in listOfemployees)
- {
- Console.WriteLine(emp.GetEmployeeName() + " " + emp.GetEmployeeID());
- }
-
In the abov, you can see that we have sorted elements by all fields in the "Employee" class.
Source code is attached below.
This is how you can sort the array of objects. That's it for this article. I hope you guys enjoyed this article.
If there are any changes or anything needs to be added, please feel free to comment below.
Thank you !!!