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:
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace genericUserDefined
- {
- public class Employee : IComparable<Employee> //Icomparable interface
- {
- public int eid { get; set; }
- public string first_name { get; set; }
- public string last_name { get; set; }
- public string department { get; set; }
- public double salary { get; set; }
- public int CompareTo(Employee other) //CompareTo method implementation
- {
- if (this.eid > other.eid)
- return 1;
- else if (this.eid < other.eid)
- return -1;
- else
- return 0;
- }
- }
- }
- // Main class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace genericUserDefined
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Employee> elist = new List<Employee>();
- Employee e1 = new Employee();
- e1.eid = 101;
- e1.first_name = "Sujan";
- e1.last_name = "Hasan";
- e1.department = "IT";
- e1.salary = 55000.00;
- elist.Add(e1);
- Employee e2 = new Employee();
- e2.eid = 102;
- e2.first_name = "Shakil";
- e2.last_name = "Sorker";
- e2.department = "Networking";
- e2.salary = 75000.00;
- elist.Add(e2);
- Employee e3 = new Employee();
- e3.eid = 100;
- e3.first_name = "Shimul";
- e3.last_name = "Hossain";
- e3.department = "Marketing";
- e3.salary = 175000.50;
- elist.Add(e3);
- print(elist);
- Console.WriteLine("\nAfter sorting the value with respect of employee id:\n");
- elist.Sort();
- print(elist);
- Console.ReadKey();
- }
- public static void print(List<Employee> emp)
- {
- foreach (Employee obj in emp)
- {
- Console.WriteLine(obj.eid+" "+obj.first_name+" "+obj.last_name+" "+obj.department+" "+obj.salary);
- }
- }
- }
- }
0 Comments:
Post a Comment