C# Generic Collections Part 6: Comparison Delegate

Comparison delegate is used to sort or order the data inside a collection. It takes two parameters as generic input type and return type should always be int. This is how we can declare Comparison delegate.

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>  
  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)  
  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.     public class compareEmployee : IComparer<Employee>  
  28.     {  
  29.   
  30.         public int Compare(Employee x, Employee y)  
  31.         {  
  32.             if (x.salary > y.salary)  
  33.                 return 1;  
  34.             else if (x.salary < y.salary)  
  35.                 return -1;  
  36.             else  
  37.                 return 0;  
  38.         }  
  39.     }  
  40. }  
  41.   
  42.   
  43. //Main class  
  44.   
  45. using System;  
  46. using System.Collections.Generic;  
  47. using System.Linq;  
  48. using System.Text;  
  49. using System.Threading.Tasks;  
  50.   
  51. namespace genericUserDefined  
  52. {  
  53.     class Program  
  54.     {  
  55.   
  56.         public static int compareNames(Employee n1, Employee n2)  
  57.         {  
  58.             return n1.first_name.CompareTo(n2.first_name);  
  59.         }  
  60.   
  61.         static void Main(string[] args)  
  62.         {  
  63.             List<Employee> elist = new List<Employee>();  
  64.   
  65.             Employee e1 = new Employee();  
  66.             e1.eid = 101;  
  67.             e1.first_name = "Sujan";  
  68.             e1.last_name = "Hasan";  
  69.             e1.department = "IT";  
  70.             e1.salary = 55000.00;  
  71.   
  72.             elist.Add(e1);  
  73.   
  74.             Employee e2 = new Employee();  
  75.             e2.eid = 102;  
  76.             e2.first_name = "Shakil";  
  77.             e2.last_name = "Sorker";  
  78.             e2.department = "Networking";  
  79.             e2.salary = 175000.00;  
  80.   
  81.             elist.Add(e2);  
  82.   
  83.             Employee e3 = new Employee();  
  84.             e3.eid = 100;  
  85.             e3.first_name = "Shimul";  
  86.             e3.last_name = "Hossain";  
  87.             e3.department = "Marketing";  
  88.             e3.salary = 75000.50;  
  89.             elist.Add(e3);  
  90.   
  91.               
  92.   
  93.             print(elist);  
  94.             Console.WriteLine("\nAfter sorting the value with respect of employee id:\n");  
  95.             elist.Sort();  
  96.             print(elist);  
  97.   
  98.             Console.WriteLine("\nSorting with respect to salary:\n");  
  99.             compareEmployee obj = new compareEmployee();  
  100.             elist.Sort(obj);  
  101.             print(elist);  
  102.   
  103.             Comparison<Employee>obj1=new Comparison<Employee>(compareNames);//Using comparison delegate  
  104.             Console.WriteLine("\nSorting with respect to first_name:\n");  
  105.             elist.Sort(obj1);  
  106.             print(elist);  
  107.   
  108.             Console.ReadKey();  
  109.         }  
  110.         public static void print(List<Employee> emp)  
  111.         {  
  112.             foreach (Employee obj in emp)  
  113.             {  
  114.                 Console.WriteLine(obj.eid+" "+obj.first_name+" "+obj.last_name+" "+obj.department+" "+obj.salary);  
  115.             }  
  116.         }  
  117.     }  
  118. }  
Share:

0 Comments:

Post a Comment