通过多个键值对数组进行排序[英] Sorting array by multiple key values

本文是小编为大家收集整理的关于通过多个键值对数组进行排序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

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

推荐答案

使用 ThenBy :

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

ThenByDescending :

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

其他推荐答案

您可以做

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

其他推荐答案

尝试以下方法:

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

本文地址:https://www.itbaoku.cn/post/1556807.html

问题描述

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)

推荐答案

Use ThenBy:

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

Or ThenByDescending:

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

其他推荐答案

You could do

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

其他推荐答案

Try this:

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