C# Collections Part 4: Using Icomparable Interface

In the previous content we using a list to store employee record and the type of the list was complex.
Here the result we get, it was not in a sorted order.Now if we want to sort the list using sort() method, we get an error because of  existence of different types of object. Get rid of this problem we use Icomparable Interface.

The role of IComparable is to provide a method of comparing two objects of a particular type. This is necessary if you want to provide any ordering capability for your object. Think of IComparable as providing a default sort order for your objects. For example, if you have an array of objects of your type, and you call the Sort method on that array, IComparable provides the comparison of objects during the sort. When you implement the IComparable interface, you must implement the CompareTo method.


Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace genericUserDefined  
  8. {  
  9.     public class Employee : IComparable<Employee> //Icomparable interface   
  10.     {  
  11.         public int eid { get; set; }  
  12.         public string first_name { get; set; }  
  13.         public string last_name { get; set; }  
  14.         public string department { get; set; }  
  15.         public double salary { get; set; }  
  16.   
  17.         public int CompareTo(Employee other)  //CompareTo method implementation   
  18.         {  
  19.             if (this.eid > other.eid)  
  20.                 return 1;  
  21.             else if (this.eid < other.eid)  
  22.                 return -1;  
  23.             else  
  24.                 return 0;  
  25.         }  
  26.     }  
  27. }  
  28.   
  29.   
  30. // Main class  
  31.   
  32. using System;  
  33. using System.Collections.Generic;  
  34. using System.Linq;  
  35. using System.Text;  
  36. using System.Threading.Tasks;  
  37.   
  38. namespace genericUserDefined  
  39. {  
  40.     class Program  
  41.     {  
  42.         static void Main(string[] args)  
  43.         {  
  44.             List<Employee> elist = new List<Employee>();  
  45.   
  46.             Employee e1 = new Employee();  
  47.             e1.eid = 101;  
  48.             e1.first_name = "Sujan";  
  49.             e1.last_name = "Hasan";  
  50.             e1.department = "IT";  
  51.             e1.salary = 55000.00;  
  52.   
  53.             elist.Add(e1);  
  54.   
  55.             Employee e2 = new Employee();  
  56.             e2.eid = 102;  
  57.             e2.first_name = "Shakil";  
  58.             e2.last_name = "Sorker";  
  59.             e2.department = "Networking";  
  60.             e2.salary = 75000.00;  
  61.   
  62.             elist.Add(e2);  
  63.   
  64.             Employee e3 = new Employee();  
  65.             e3.eid = 100;  
  66.             e3.first_name = "Shimul";  
  67.             e3.last_name = "Hossain";  
  68.             e3.department = "Marketing";  
  69.             e3.salary = 175000.50;  
  70.             elist.Add(e3);  
  71.   
  72.               
  73.   
  74.             print(elist);  
  75.             Console.WriteLine("\nAfter sorting the value with respect of employee id:\n");  
  76.             elist.Sort();  
  77.             print(elist);  
  78.   
  79.             Console.ReadKey();  
  80.         }  
  81.         public static void print(List<Employee> emp)  
  82.         {  
  83.             foreach (Employee obj in emp)  
  84.             {  
  85.                 Console.WriteLine(obj.eid+" "+obj.first_name+" "+obj.last_name+" "+obj.department+" "+obj.salary);  
  86.             }  
  87.         }  
  88.     }  
  89. }  
Share:

0 Comments:

Post a Comment