Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按列表中包含的相同类型的整数数量对列表进行排序_C#_Linq_List_Sorting - Fatal编程技术网

C# 按列表中包含的相同类型的整数数量对列表进行排序

C# 按列表中包含的相同类型的整数数量对列表进行排序,c#,linq,list,sorting,C#,Linq,List,Sorting,大家好,我有一个列表,我正试图根据它包含多少相同类型的int进行排序。列表列表中的每个项目包含4个整数,例如: [0,1,2,3] [0,1,1,1] [0,2,2,2]等。我试图根据包含最多特定int的列表进行排序。因此,如果我想对这三个int进行排序,[0,1,1,1]列表将位于顶部,因为它包含三个1等。有人告诉我使用linq来完成此操作,但我不知道如何使用linq.orderby方法。如果有正确的方法,有人能帮我吗?谢谢 public class EnemyAI : MonoBehavio

大家好,我有一个列表,我正试图根据它包含多少相同类型的int进行排序。列表列表中的每个项目包含4个整数,例如:

[0,1,2,3] [0,1,1,1] [0,2,2,2]等。我试图根据包含最多特定int的列表进行排序。因此,如果我想对这三个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;

}
公共类EnemyAI:单一行为
{
List ListofPath=新列表();
公共无效排序列表()
{
ListofPath.OrderBy(runeList=>runeList.customMethod(runeType));//符文类型是一个int
}
公共整数自定义方法(整数排序)
{
int holder=0;
for(int i=0;i
您的问题可以分为两个子问题

1.如何获取列表中最频繁整数的计数?

您可以使用以下LINQ查询:

int countOfMostOccurences = arr.GroupBy(x => x).Max(x => x.Count()); 
2.如何按特定规则降序排列列表:

list = list.SortByDescending(x => rule);
List<List<int>> lists = new List<List<int>>
{
    new List<int> { 1, 2, 3, 4 },
    new List<int> { 1, 3, 3, 3 },
    new List<int> { 1, 2, 2, 3 },
    new List<int> { 1, 1, 1, 1 }
};

lists = lists.OrderByDescending(x => x.GroupBy(g => g).Max(g => g.Count())).ToList();
现在,合并它:

list = list.SortByDescending(x => rule);
List<List<int>> lists = new List<List<int>>
{
    new List<int> { 1, 2, 3, 4 },
    new List<int> { 1, 3, 3, 3 },
    new List<int> { 1, 2, 2, 3 },
    new List<int> { 1, 1, 1, 1 }
};

lists = lists.OrderByDescending(x => x.GroupBy(g => g).Max(g => g.Count())).ToList();
列表列表=新列表
{
新名单{1,2,3,4},
新名单{1,3,3,3},
新名单{1,2,2,3},
新列表{1,1,1,1}
};
lists=lists.OrderByDescending(x=>x.GroupBy(g=>g).Max(g=>g.Count()).ToList();

您的问题可以分为两个子问题

1.如何获取列表中最频繁整数的计数?

您可以使用以下LINQ查询:

int countOfMostOccurences = arr.GroupBy(x => x).Max(x => x.Count()); 
2.如何按特定规则降序排列列表:

list = list.SortByDescending(x => rule);
List<List<int>> lists = new List<List<int>>
{
    new List<int> { 1, 2, 3, 4 },
    new List<int> { 1, 3, 3, 3 },
    new List<int> { 1, 2, 2, 3 },
    new List<int> { 1, 1, 1, 1 }
};

lists = lists.OrderByDescending(x => x.GroupBy(g => g).Max(g => g.Count())).ToList();
现在,合并它:

list = list.SortByDescending(x => rule);
List<List<int>> lists = new List<List<int>>
{
    new List<int> { 1, 2, 3, 4 },
    new List<int> { 1, 3, 3, 3 },
    new List<int> { 1, 2, 2, 3 },
    new List<int> { 1, 1, 1, 1 }
};

lists = lists.OrderByDescending(x => x.GroupBy(g => g).Max(g => g.Count())).ToList();
列表列表=新列表
{
新名单{1,2,3,4},
新名单{1,3,3,3},
新名单{1,2,2,3},
新列表{1,1,1,1}
};
lists=lists.OrderByDescending(x=>x.GroupBy(g=>g).Max(g=>g.Count()).ToList();

只需按特定数字的计数降序即可:

var list = new List<List<int>>() 
{ 
    new List<int> { 0, 1, 2, 3 }, 
    new List<int> { 0, 1, 1, 1 }, 
    new List<int> { 0, 2, 2, 2 } 
};

var result = list.OrderByDescending(c => c.Count(y => y == 1)).ToList();
var list=新列表()
{ 
新列表{0,1,2,3},
新列表{0,1,1,1},
新列表{0,2,2,2}
};
var result=list.OrderByDescending(c=>c.Count(y=>y==1)).ToList();

只需按特定数字的计数降序即可:

var list = new List<List<int>>() 
{ 
    new List<int> { 0, 1, 2, 3 }, 
    new List<int> { 0, 1, 1, 1 }, 
    new List<int> { 0, 2, 2, 2 } 
};

var result = list.OrderByDescending(c => c.Count(y => y == 1)).ToList();
var list=新列表()
{ 
新列表{0,1,2,3},
新列表{0,1,1,1},
新列表{0,2,2,2}
};
var result=list.OrderByDescending(c=>c.Count(y=>y==1)).ToList();

为什么
{0,2,2,2}
不如
{0,1,1,1}
它们都有3个相同的类型?为什么
{0,2,2,2}
不如
{0,1,1,1}
因为它们都有3个相同的类型?我是否遗漏了什么?
其中包含了大部分特定的int.
。更多:您会感到惊讶,但使用此代码,
[0,2,2,2]
将在
[0,1,1,1]
之前出现:
[0,1,2,3][0,2,2][0,1,1]
。还有一个问题:仅仅对列表进行排序不会对列表进行排序-您必须将排序结果分配给某个对象。您的解决方案看起来不错-但无法解决问题。您按照任何值的最大发生率对列表进行排序。问题是…其中包含了最多的特定int。在这种情况下,@GiorgiNakeuri的答案是正确的,并且简言之。是的,你是对的。这个解决方案适用于按任何最常见整数的出现次数进行排序。问题及其标题不清楚。如果OP需要一个数组按特定int的计数进行排序,那么他应该接受@GiorgiNakeuri的答案。似乎OP的
包含了大多数特定int
的内容不是说
以一个特定的int开头,并且在
中出现最多,甚至看不到提供的代码包含此功能。我是否遗漏了某个包含最多特定int的
。更多:您会感到惊讶,但使用此代码,
[0,2,2,2]
将在
[0,1,1]之前出现
如果它出现在列表之前:
[0,1,2,3][0,2,2][0,1,1]
。还有一个问题:仅仅对列表进行排序不会对列表进行排序-您必须将排序结果分配给某个对象。您的解决方案看起来不错-但无法解决问题。您按照任何值的最大发生率对列表进行排序。问题是…其中包含了最多的特定int。在这种情况下,@GiorgiNakeuri的答案是正确的,并且简言之。是的,你是对的。这个解决方案适用于按任何最常见整数的出现次数进行排序。问题及其标题不清楚。如果OP需要一个数组按特定int的计数进行排序,那么他应该接受@GiorgiNakeuri的答案。似乎OP的
包含了大多数特定int
的内容并不意味着
以特定的int开头,并且在
中最多出现,甚至看不到提供的代码包含此功能。谢谢!效果很好:)但是,有一件事,到底什么才算(y=>y==1)do?
c
是内部列表,
y
是c中的元素。因此它计算等于
1
的元素。谢谢!非常有用:)但是,Count(y=>y==1)到底做了什么?
c
是内部列表,
y
是c中的元素。因此它计算等于
1
的元素。