Chase Ernst
Chase Ernst

Reputation: 1155

Sorting a list of enumerations

So I have an enumeration

public enum CardValue
{
    Deuce = 2, Three, Four.... //And so on..
}

Then I also have a structure

public struct Card
{
   public CardSuit S;
   public CardValue CV;

   public Card(CardSuit Suit, CardValue value)
   {
       S = suit;
       CV = value;
   }
}

Then I populated my list with the some card values (all random). I am trying to sort them so I can have the value from highest to lowest, and I can't seem to get it right. What I am trying is:

List<Card> Hand1 = new List<Deck>();
Hand1.Sort(CardVale.Ace => CardValue.Duece)

I was just wondering what is wrong. If anyone would be able to help me that would be greatly appreciated.

Upvotes: 0

Views: 274

Answers (1)

Neil
Neil

Reputation: 1633

Sort takes an expression that converts the class in list to the property you want to sort by

Hand1.Sort(x=>x.CV)

An expression like x=>x.CV is the equivelent of

private static CardValue GetFieldToSortOn(Card x) { return x.CV; }

Upvotes: 1

Related Questions