2
foreach(Unit enemy in enemies.OrderByDescending(enemy=>enemy.CurrentHP))
{
    Events.UnitDamaged(enemy);
    return;
}

How to pass multiple key values to Sorting? Something like

enemies.OrderByDescending(enemy => enemy.CurrentHP && enemy.MaximumHP)
3
  • You want to sort by one property and then another? Commented Sep 10, 2013 at 11:44
  • 1
    do you mean something like enemies.OrderByDescending(enemy => enemy.CurrentHP).ThenBy(enemy => enemy.MaximumHP) ? Commented Sep 10, 2013 at 11:44
  • 1
    also available: ThenByDescending... Commented Sep 10, 2013 at 11:44

3 Answers 3

5

Use ThenBy:

enemies.OrderByDescending(enemy => enemy.CurrentHP)
       .ThenBy(enemy => enemy.MaximumHP)

Or ThenByDescending:

enemies.OrderByDescending(enemy => enemy.CurrentHP)
       .ThenByDescending(enemy => enemy.MaximumHP)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

enemies.OrderByDescending(enemy => enemy.CurrentHP)
       .ThenByDescending(enemy1 =>  enemy1.MaximumHP)

Comments

0

You could do

enemies.OrderByDescending(enemy => enemy.CurrentHP)
       .ThenByDescending(enemy => enemy.MaximumHP)

1 Comment

This will just apply the second sort, and ignore the first one (although, performancewise, it will also do the first one). You need ThenBy or ThenByDescending, methods of the IOrderedEnumerable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.