Click here to Skip to main content
15,030,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
/// <summary>
/// Create a new class that implements IComparer(Of IPerson)
/// This class should compare the Name property of IPerson objects, such that objects are ordered like: 
/// "Alice", "Bob", "Charles"
/// </summary> 

public IComparer<iperson> GetPeopleNameComparer()
{
 //throw new NotImplementedException("Remove this line and write some code!");

            List<iperson> list = new List<iperson>();
            list.Add(new Employee() { Name = "F", DOB = DateTime.ParseExact("01-01-2001", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "G", DOB = DateTime.ParseExact("01-01-2002", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "H", DOB = DateTime.ParseExact("01-01-2009", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "A", DOB = DateTime.ParseExact("01-01-2003", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "V", DOB = DateTime.ParseExact("01-01-2004", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "B", DOB = DateTime.ParseExact("01-01-2005", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            return list.Sort(); // Im getting error here.Can anyone please help me.
}
Posted
Updated 21-Oct-14 0:40am
v3

C#
List<iperson> list = new List<iperson>();

here you are creating <list> Of type iperson but while adding values to the list you are assigning values of Employee type. Then you are calling sort() on the list. Solution for this add iperson type values to the list and the call sort().
   
v2
Comments
Member 8361929 21-Oct-14 7:20am
   
IComparer(iperson) - How will be return type of this ?

Can you please write any sample code for this?
johannesnestler 21-Oct-14 7:56am
   
No - it Looks like Employee implements iperson Interface or is derived from it - your conclusion is wrong.
Hmm - read the error message again...
Your method signature requires to return an IComparer for iperson the list.Sort() method returns nothing (void)..
What you do here is creating and sorting a List you don't obtain or create a Comparer!
Though, You can give the Sort method a comparer you want to use.
As far as I understand your requirement you should return a special Comparer implementation not create a list nor sort it.
Read http://msdn.microsoft.com/de-de/library/cfttsh47(v=vs.110).aspx[^]
You find an example how to implement and use different Comparers there.
   
Comments
Member 8361929 21-Oct-14 7:29am
   
IComparer(iperson) - How will be return type of this ?

Can you please write any sample code for this?
johannesnestler 21-Oct-14 7:42am
   
Pleas read the MSDN - docu - should I copy the code from the example there? Please clarify what is your task - I don't like doing guess work. Name of the method implies you should return a custom comparer - so write one and return it. If this is not what you should do - please say it!
You have to call .ToList after sort since sort doesn't return a value.
   
v5
Comments
johannesnestler 21-Oct-14 6:54am
   
Sorry, but this is complete nonsense...
Sinisa Hajnal 21-Oct-14 6:58am
   
Which part? Sort returns void. He's returning the result of the sort. And you're saying the same thing in your solution. I just posted first version too fast withour really looking at the code. And Sort does NOT require a comparer if you're happy with the default one.

I should start reading more carefuly - return type should be the list, not the comparer too, good catch.
johannesnestler 21-Oct-14 7:40am
   
This is obvious homework - read the comment, read the name of the method. Task is: create a custom comparer - this is just the method to return it. so it has nothing todo with sort itself (this code may be used to call List.Sort with a custom comparer) - calling ToList?
Sinisa Hajnal 21-Oct-14 8:00am
   
Ah, I see. I commented on returning the list, the method looks like it should return sorted list. If he returns the comparer, then there shouldn't even be the list in the method :)
johannesnestler 21-Oct-14 8:43am
   
Exactly
Member 8361929 21-Oct-14 7:29am
   
IComparer(iperson) - How will be return type of this ?

Can you please write any sample code for this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900