c# - Need help understanding how the List<T>.Sort(IComparer(T)) knows the value to sort on -
I am working on a program (learning C #) which creates an object (card) and in the program the deck Like the card from the value which is amazed by the fact that it is able to sort the card deck through the value of the card, when the example of the object is not the passed parameter of the method, but the class object Below is the code to help answer my question - if I were passing the instant object then I would complete it.
Class to create an object:
class card {public suit suit {get; Set; } Receive public value values; Set; } Public card (suit of suit, value value) {this.Suit = suit; this. Value = value; } Public string name {get {return Value.ToString () + "of" + suit.toString (); }} Public override string toasting () {return name; }}
In the second category, I call that method that instantizes an object (CardComparer_byValue) that comes from the interface ICPPrier. This is where my illusion comes in.
Sort public zeros () {cards.Sort (new CardComparer_byValue ()); }
and SortByValue Class
class CardComparer_byValue: IComparer & lt; Cards & gt; {Compare Public Entry (Card X, Card Y) {if (x.Value & gt; y.Value) Return 1; If (X. Vel and LT; Y.Well) returns -1; If (x.Suit> Y.Suit) is back 1; If (x.Suit & lt; y.Suit) return -1; Return 0; }}
I think the above method "sort bad val" was called in this way
card miccard = new card ("spades" , 4); Public Zero Sort () {cards.Sort (New CardComparer_byValue (mycard)); }
So will someone tell me how I can work in this way, what is missing on this?
This is not organizing the entire archive, not against a specific "card".
This is the reason that the compare
is defined as the method:
This "card x" and "card y ", Which it uses to card
collection, sort
method to sort it compare
method several times , Is using it to sort the entire collection by comparing the pairs in pairs at a time. / P>
Comments
Post a Comment