Search code examples
c#sortingplaying-cards

c# sort hand of playing cards


I have a class Card with the following properties

public int value { get; private set; }
public string suit { get; private set; }

Once I have a player with a hand

List<Card> hand

How do I sort that hand, so that

1) primarily sorted into suits, with the largest (most cards) groups first, and smallest (least cards) groups last, and

2) withing those groups, sort them from high to low

Also, this is for Rook - almost identical to playing cards. values range from 1-14, with four suits - Red, Green, Yellow, and Black. In addition, there is one "Rook" card. Just a heads up in case this changes anything.


Solution

  • var sorted = hand
        .GroupBy(l => l.Suit)
        .OrderByDescending(g => g.Count())
        .SelectMany(g => g.OrderByDescending(c => c.Value));