Home > Back-end >  How do I sort card objects by their id
How do I sort card objects by their id

Time:06-09

Suppose I have a hand (list) of 8 card objects, the cards are unordered. Each card object in the deck has an id from 1 to 52. but the deck is shuffled then 8 cards are dealt to me. Here are the things I have access to:

hand // a list of 8 card objects
hand[0].GetCardId() // returns the id of first card in my hand

Having these two things, how do you sort my hand so that the first card in my hand has the smallest id and the last card in my hand has the biggest id?

CodePudding user response:

sort my hand so that the first card in my hand has the smallest id and the last card in my hand has the biggest id?

With LINQ

hand.OrderBy(c => c.GetCardId());

With Array class

Array.Sort(hand, (a, b) => a.GetCardId() - b.GetCardId());
  • Related