Tuesday, March 12, 2013

.NET interview questions and answers: - What is difference betweenIcomparable VS Icomparer ?

Pre-requisite:- Please read regarding “IComparable” interface from .NET interview questions: - What is “IComparable”?.

If you read the above article properly,“IComparable” interface helps you to implement a default sort implementation for thecollection.  But what if we want to sort using multiple criteria’s?.For those instances “IComparable” has a limitation and “IComparer” is the guy.

For example if we want to sort by “Name” under some situation and sort by “Age” under some other situations we need to implement “IComparer” interface.

So the first step is to create different classes for each sort criteria. These classes will implement “IComparer” interface and will have sorting logic defined in the “Compare” method. You can see we have two different classes defined “CompareByName” and “CompareByAge”. One compares on the basis of “name” and the other on the basis of “age”.


class CompareByName : IComparer
    {
        public int Compare(Person x, Person y)
        {
            return string.Compare(x.Name, y.Name);
        }
    }
    class CompareByAge : IComparer
    {
        public int Compare(Person x, Person y)
        {
            if (x.Age > y.Age) return 1;
            if (x.Age < y.Age) return -1;
             return 0;
        }
    }

If you see the logic for “CompareByName” its simple. It uses the string comparison to evaluate the “Name” value sorting. But when we talk about numeric comparison there are 3 possible outputs Greater than , less than or Equal. So if you see “CompareByAge” class it returns 3 values 1 ( Greater than) , -1 ( Less than ) and 0 ( for Equal to).

Now that we have created the separate logics we can now invoke the “Peoples’ list by passing the class object. So for example if we want to sort by name, you will use the below code snippet.


Peoples.Sort(new CompareByName());

The output of the above code is nothing but alphabetically sorted data.

Ajay  20
Loki  40
Madan  20
Raju  20
Shiv  20
Sukesh  30

If you invoke the sort method of the list by using the age logic it will throw an output sorted on age value.

Peoples.Sort(new CompareByAge());

Ajay  20
Raju  20
Shiv  20
Madan  20
Sukesh  30
Loki  40

So the difference is really default internal implementation or customizable external implementation.  When we use “IComparable” we can have only one default sorting logic and that logic goes inside the collection itself. For “IComparator” the logic is outside the collection , in other words more extensible and the collection is not disturbed.


For further c# interview preparation you can go through the following articles

Huge collection of c# interview question videos: - .NET interview question videos .

Read this article on how to prepare for c# interviews: - c# interview questions and answers

Huge collection of c# and .NET interview question videos: - c# and .NET interview question videos.

Do forget SQL Server interview questions and answers on clustered and non-Clustered indexes. 



The above question is taken from .NET interview question book published by BPB publications.















No comments: