Solved

# IComparer<T>

Posted on 2013-10-20
Medium Priority
185 Views
Last Modified: 2013-10-23
I have the following code bellow, could someone explain me how this result is calculated from these classes.
A 13 inch Decoy
A 14 inch Decoy
A 18 inch Muscovy
A 11 inch Muscovy
A 14 inch Mallard
A 17 inch Mallard
End of ducks!

Main class

static void Main(string[] args)
{

List<Duck> ducks = new List<Duck>(){
new Duck() { Kind = KindOfDuck.Mallard, Size = 17 },
new Duck() { Kind = KindOfDuck.Muscovy, Size = 18 },
new Duck() { Kind = KindOfDuck.Decoy, Size = 14 },
new Duck() { Kind = KindOfDuck.Muscovy, Size = 11 },
new Duck() { Kind = KindOfDuck.Mallard, Size = 14 },
new Duck() { Kind = KindOfDuck.Decoy, Size = 13 },

};

DuckComparerByKind kindComparer = new DuckComparerByKind();
ducks.Sort(kindComparer);
PrintDucks(ducks);

}

public static void PrintDucks(List<Duck> ducks)
{
foreach (Duck duck in ducks)
{
Console.WriteLine(duck);

}

Console.WriteLine("End of ducks!");
Console.ReadLine();
}

}

class DuckComparerByKind.cs
namespace ConsoleApplication1
{
class DuckComparerByKind : IComparer<Duck>
{
public int Compare(Duck x, Duck y)
{

if(x.Kind < y.Kind)
return -1;
if (x.Kind > y.Kind)
return 1;
else
return 0;
}

}
}

class Duck
class Duck : IComparable<Duck>
{
public int Size;
public KindOfDuck Kind;

public int CompareTo(Duck duckToCompare)
{
if (this.Size > duckToCompare.Size)
return 1;
else if (this.Size < duckToCompare.Size)
return -1;
else
return 0;

}

public override string ToString()
{
return "A " + Size + " inch " + Kind.ToString();
}

}

enumaration KindOfDuck

enum KindOfDuck
{
Decoy,
Muscovy,
Mallard,

}
0
Question by:yguyon28
• 2
• 2
4 Comments

LVL 75

Expert Comment

ID: 39586898
The integer that the Compare method returns has the following characteristics:

If less than zero, then the instance is considered to be less than the duckToCompare
If zero, then both the instance and the duckToCompare are the same
If greater than zero, then the instance is considered to be greater than the duckToCompare

This is how Sort figures out how to place the ducks during its operation. So what constitutes one duck being less than, greater than, or equal to? Well, by your code it is the Kind property. Since the Kind property is defined to be an enum (named KindOfDuck) it is of an integral type--meaning the underlying data type is an integer. So one duck being greater than another comes down to one number being larger than another. With enums, if you do not specify an explicit value, the compiler will start numbering the first item as zero, and then increment by one. So your ducks are valued at:

Decoy: 0
Muscovy: 1
Mallard: 2

So whichever number is less during the sort will end up toward the front of the list (and vice versa).
0

LVL 45

Accepted Solution

AndyAinscow earned 1500 total points
ID: 39586970
One thing that kaufmed seems not to have mentioned.
You are supplying a function written by you that the sort method uses rather than some default method.

DuckComparerByKind kindComparer = new DuckComparerByKind();
ducks.Sort(kindComparer);
0

LVL 75

Expert Comment

ID: 39596312
I'd really prefer an explanation on this answer selection as to why my comment wasn't (apparently) helpful at all.
0

LVL 45

Expert Comment

ID: 39596327
I agree.  My comment was just to show how the custom sort function was actually being called in the code supplied, kaufmed explained the function itself.
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
###### Suggested Courses
Course of the Month12 days, 12 hours left to enroll

#### 622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.