0

I have an interface named IEmployee. I need to implement the IComparer to the implementation of my interface.

Here is my code,

interface IEmployee
  {

      String Name {get;set;}
      DateTime DOB {get;set;}
  }

I have created a child class for this like

class Employee
  {

      String Name {get;set;}
      DateTime DOB {get;set;}
  }

Now i need to implement the IComparer to this, and in my Main i want to get the copmarer like

IComparer<IEmployee> comparerobject= // The comparer from the class.

And using this comparer i need to sort the collection of employee based on their names, like

var employees = new IEmployee[]{new Employee("....."),....}
Array.Sort<IEmployee>(employees , comparer);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
Thorin Oakenshield
  • 14,232
  • 33
  • 106
  • 146

3 Answers3

10

you can do

var sorted = employees.OrderBy(e => e.Name).ToArray();

or with a comparer

public class EmployeeComparer : IComparer<IEmployee>
    {
        public int Compare(IEmployee x, IEmployee y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

then Array.Sort(employees, new EmployeeComparer());

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
9

You could instead implement IComparable. Then your code could look something like:

interface IEmployee : IComparable
{
    String Name { get; set; }
    DateTime DOB { get; set; }
}

class Employee : IEmployee
{
    public string Name { get; set; }
    public DateTime DOB { get; set; }

    public int CompareTo(object obj)
    {
        return this.Name.CompareTo(((IEmployee)obj).Name);
    }
}

Then you can just sort the array as follows:

Array.Sort<IEmployee>(employees);
BryanJ
  • 8,485
  • 1
  • 42
  • 61
  • I think you meant "You could just have your [class] implement IComparable" (i.e. not "interface"). – J0e3gan May 10 '13 at 04:06
  • 1
    @J0e3gan Yes, I should have worded that more carefully. The class would contain the implementation, but how would you explain it if the implementation of an interface required you also implement another interface (as in my answer above)? – BryanJ May 10 '13 at 13:17
  • No worries - was just trying to help clarify a good answer. I guess I would say "You could just define an appropriate interface that inherits `IComparable` - e.g. `IEmployee` - and then have your class implement it." – J0e3gan May 11 '13 at 16:51
  • 1
    It is interesting that even MSDN documentation on C# interfaces has muddied language in this respect - using "inherit" in [VS-2008-era documentation](http://msdn.microsoft.com/en-us/library/ms173156(v=VS.80).aspx) ("Interfaces can _inherit_ other interfaces.") and "implement" in [VS-2012-era documentation](http://msdn.microsoft.com/en-us/library/ms173156(v=vs.110).aspx) ("Interfaces can _implement_ other interfaces.") with respect to a relationship between interfaces like you outline between `IComparable` and `IEmployee`. We just can't beat language's inherent ambiguity sometimes. :} – J0e3gan May 11 '13 at 16:54
2

Is there a reason you want to use an IComparer? You have several options other than that.

First of all, you can use a Linq query as described by @Keith. That has some memory implications since a new IEnumerable must be allocated.

Perhaps your best option is to use the overload of the Sort method that takes in a Comparison object, which is just a delegate that compares two objects:

Array.Sort(employees, (a, b) => a.Name.CompareTo(b.Name));

If you really want to use IComparer, then you need to make a class that implements IComparer<IEmployee>:

public class EmployeeComparer : IComparer<IEmployee>
{
    public int Compare(IEmployee e1, IEmployee e2)
    {
        return e1.Name.CompareTo(e2.Name);
    }
}

And then use that for the Sort method. You could also make Employee itself implement IComparer if you so desired, but that is strange, since then it could really just implement IComparable.

Ben Reich
  • 16,222
  • 2
  • 38
  • 59