Tuesday, November 10, 2015

IComparable & IComparer Interfaces

IComparable & IComparer Interfaces are used to provide comparison mechanism to collection of objects that comparison scheme is used by the collection for sorting functions.

IComparable: It defines a way of comparison for two similar class objects. A collection of objects internally uses this defined way to sort objects.
Using IComparable we can define only one way of comparison for a collection of objects.
This is internal comparer
We defines CompareTo method of IComparable  that returns int
            public int CompareTo(object obj)
            {
                return this.Age.CompareTo(((Person)obj).Age);
            }

IComparer: It defines multiple ways of comparison for two similar class objects.
Using IComparer we can define multiple ways of comparison for a collection of objects.
This comparison take place externally
We defines Compare method of IComparer that returns int
            public int Compare(Student x, Student y)
            {
                return string.Compare(x.Name, y.Name);
            }


Source Code:
        #region IComparable
        public class Person : IComparable
        {
            public string Name { get; set; }
            public int Age { get; set; }

            public int CompareTo(object obj)
            {
                return this.Age.CompareTo(((Person)obj).Age);
            }
        }

        //Generic Icomparable example
        public class Employee : IComparable<Employee>
        {
            public string Name { get; set; }
            public int Salary { get; set; }
           
            public int CompareTo(Employee other)
            {
                return this.Salary.CompareTo(other.Salary);
            }
        }
        #endregion

        #region IComparer
        public class Student
        {
            public string Name { get; set; }
            public double Percentile { get; set; }
        }

        public class SortByName : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                return string.Compare(x.Name, y.Name);
            }
        }
        public class SortByPercentile : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                if (x.Percentile == y.Percentile)
                    return 0;
                else if (x.Percentile > y.Percentile)
                    return 1;
                else
                    return -1;
            }
        }

        #endregion

Client Code:
        static void Main(string[] args)
        {
            #region IComparable
            Person me = new Person { Name = "Anand", Age = 32 };
            Person MyFather = new Person { Name = "PSTomar", Age = 65 };
            Person MyGrandFather = new Person { Name = "DSTomar", Age = 85 };

            List<Person> lstPerson = new List<Person>();

            lstPerson.Add(MyFather);
            lstPerson.Add(me);
            lstPerson.Add(MyGrandFather);

            lstPerson.Sort();
            Console.WriteLine("Result of IComparable example :");
            foreach (Person p in lstPerson)
                Console.WriteLine("Name : {0}, Age : {1} ", p.Name, p.Age);
            Console.ReadLine();
            #endregion

            #region IComparer
            Student Megha = new Student { Name = "Megha", Percentile = 32.55 };
            Student Renuka = new Student { Name = "Renuka", Percentile = 65.12 };
            Student Ghanshyam = new Student { Name = "Ghanshyam", Percentile = 80.11 };

            List<Student> lstStudent = new List<Student>();

            lstStudent.Add(Megha);
            lstStudent.Add(Renuka);
            lstStudent.Add(Ghanshyam);

            lstStudent.Sort(new SortByName());
            Console.WriteLine("Result of IComparer example :");
            foreach (Student p in lstStudent)
                Console.WriteLine("Name : {0}, Percentile : {1} ", p.Name, p.Percentile);
            Console.ReadLine();
           
            lstStudent.Sort(new SortByPercentile());
            foreach (Student p in lstStudent)
                Console.WriteLine("Name : {0}, Percentile : {1} ", p.Name, p.Percentile);
            Console.ReadLine();
            #endregion
        }

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...