【发布时间】:2015-11-11 05:47:55
【问题描述】:
大家好,我有一个列表列表,我试图根据它包含多少相同类型的 int 对其进行排序。列表列表中的每个项目都包含 4 个整数,例如:
[0,1,2,3] [0,1,1,1] [0,2,2,2] 等。我试图根据包含大多数特定 int 的列表对列表进行排序。因此,如果我想对这三个进行排序,则 [0,1,1,1] 列表将位于顶部,因为它包含三个 1 等。有人告诉我使用 linq 来执行此操作,但不知道如何使用 linq.orderby一个方法。如果有正确的方法,有人可以帮助我吗?谢谢!
public class EnemyAI : MonoBehaviour
{
List<List<int>> listOfPaths = new List<List<int>>();
public void sortListofLists ()
{
listOfPaths.OrderBy(runeList => runeList.customMethod(runeType)); //rune type is an int
}
public int customMethod(int sort)
{
int holder = 0;
for (int i = 0; i < MethodList.Count; i++)
{
if (MethodList[i] == sort)
{
holder++;
}
}
return holder;
}
【问题讨论】:
-
为什么
{ 0, 2, 2, 2 }不如{ 0, 1, 1, 1 }好,因为他们都有 3 个?