tags:

views:

86

answers:

3

I am working on a program (learning C#) that creates a object (Card) and in the program it sorts the deck of cards by value. What I am baffled by is how it is able to sort a deck of cards by the value of the cards when the instance of the object isn't the parameter passed to the method but the class object. Below is the code to help answer my question - I would complete follow this if I was passing the instantiated object.

The class to create the object:

class Card
{
    public Suits Suit { get; set; }
    public Values Value { get; set; }

    public Card(Suits suit, Values value)
    {
        this.Suit = suit;
        this.Value = value;
    }

    public string Name
    {
        get { return Value.ToString() + " of " + Suit.ToString(); }
    }

    public override string ToString()
    {
        return Name;
    }
}

In another class I am calling the method that instantiate a object(CardComparer_byValue) that inherits from interface IComparer. This is where my confusion comes in.

public void SortByValue() {
cards.Sort(new CardComparer_byValue());
} 

And the SortByValue Class

class CardComparer_byValue : IComparer<Card>
{
    public int Compare(Card x, Card y)
    {
        if (x.Value > y.Value)
            return 1;
        if (x.Value < y.Value)
            return -1;
        if (x.Suit > y.Suit)
            return 1;
        if (x.Suit < y.Suit)
            return -1;
        return 0;
    }

} 

I would think the above method "SortByValue" was called like this

Card mycard = new Card("Spades", 4);
public void SortByValue() {
cards.Sort(new CardComparer_byValue(mycard));
} 

So would someone explain to me what I am missing on how the sort can work this way?

+1  A: 

A class that implements IComparer<T> is required to have a method Compare(T x, T y), which Sort calls repeatedly during its sort operations.

Since Sort is on the List<T>, you have more than one card that you're dealing with. List<T> uses the Array.Sort method, which employs a QuickSort algorithm to sort them.

R. Bemrose
+5  A: 

It's sorting the entire collection, not against a specific "card".

That's why the Compare method is defined like:

public int Compare(Card x, Card y)

It uses "Card x" and "Card y", calling this on pairs of elements within the cards collection, in order to sort it. The Sort method calls this Compare method many times, using it to sort the entire collection by comparing elements in pairs one pair at a time.

Reed Copsey
A: 
new CardComparer_byValue(mycard));

Here you are not calling a method -- you are attempting to construct an object. The class does not define a constructor which takes a Card object, so you'll get a syntrax error when compiling.

The important thing to note here is that the IComparer object is just that, an object. It's like a little judge to determine the order. ("I'd like you to reorder my cards, and here's Mr. Bernstein to tell you what order to be them in.")

So, someplace deep inside the Sort method, there a line like:

  if (cmp.Compare(lhsCard, rhsCard) > 0)
       /* swap lhsCard & rhsCard */
James Curran