按多个键值排序数组

时间:2013-09-10 11:43:11

标签: c# arrays linq sorting

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

如何将多个键值传递给排序? 像

这样的东西
enemies.OrderByDescending(enemy => enemy.CurrentHP && enemy.MaximumHP)

3 个答案:

答案 0 :(得分:5)

使用ThenBy

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

ThenByDescending

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

答案 1 :(得分:0)

你可以做到

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

答案 2 :(得分:0)

试试这个:

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