Reputation: 2498
I have this object (player) with the int properties "pVictories" and "Indicator". There's a list of players, and I am going to "seed" them based on the number of victories they have, i.e. order them. So, whoever has the most victories gets the 0 spot in the list, the next most the 1 spot, etc.
If I wanted to order it that way, then great, I know what to do. However, I'm stuck on how to indicate to the order by how to react if they have the same number of victories. What's supposed to happen is I want them to compare the two indicator properties, and give the higher spot to the person with the higher indicator int. Say two players both have 4 victories, but player 1 has an indicator value of "4" and the other "2", I want the player with 4 to get the higher spot.
How would I adjust this line, or add a line to make that happen? Here's what I have:
//Sort a list based on number of Victories
List<Player> tempList = listOfPlayers.OrderBy(o => o.pVictories).ToList();
tempList.Reverse();
Would it be possible to add another, lesser condition in orderby? As in, also add in after the o.pVictories part? Or should I look into creating a custom comparer?
Upvotes: 1
Views: 42
Reputation: 149538
I think what you want is both OrderByDescending
and ThenByDescending
:
List<Player> tempList = listOfPlayers.OrderByDescending(o => o.pVictories)
.ThenByDescending(p => p.Indicator)
.ToList();
Upvotes: 4
Reputation: 486
List<Player> tempList = listOfPlayers.OrderByDescending(o => o.pVictories).ThenByDescending(x => x.Indicator).ToList();
Or use a query syntax
List<Player> tempList = (from x in listOfPlayers
orderby x.pVictories descending, x.Indicator descending
select x).ToList();
Upvotes: 2
Reputation: 12618
You can use ThenBy
extension:
List<Player> tempList = listOfPlayers.OrderByDescending(o => o.pVictories).ThenByDescending(p=>p.Indicator).ToList();
Upvotes: 3