# IComparer<T>

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!");
}

}

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,

}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Freelance programmer / ConsultantCommented:
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

Experts Exchange Solution brought to you by