【发布时间】:2011-11-15 17:31:32
【问题描述】:
如何按降序对通用列表中的相同值字段进行排序。
例子:
List<int> objList = new List<int>();
objList.Add(1);
-> objList.Add(0);
-> objList.Add(0);
objList.Add(2);
-> objList.Add(0);
不知何故这是我的来源,我想以降序模式对例如零值进行排序。
我使用此代码对数字进行排序(实际上是深度),上面的示例与此无关,但不知何故它是相同的。在我的通用列表中,我有几个深度,它们可能彼此相同,我想对相同的字段进行降序排序。
Objects.Sort(
delegate(Classes.Object.GameObject Object1, Classes.Object.GameObject Object2)
{
return Object1.Depth.CompareTo(Object2.Depth);
}
);
答案:将来可能会帮助某人
// Reverse same oredered
CurrentSameOrderedFind = Objects[0].Depth;
CurrentSameOrderedID = 0;
for (int i = 1; i <= Objects.Count - 1; i++)
{
if (Objects[i].Depth != CurrentSameOrderedFind)
{
SameOrederedFound = true;
Objects.Reverse(CurrentSameOrderedID, i - 1);
CurrentSameOrderedFind = Objects[i].Depth;
CurrentSameOrderedID = i;
}
}
if (!SameOrederedFound)
{
Objects.Reverse();
}
【问题讨论】:
-
你的问题很不清楚。例如,“上面的例子与此无关,但不知何故它是相同的”——这是什么意思?如果您能更清楚地解释问题,我们更有可能为您提供帮助。
-
:) 我不知道我应该扩展多少。很简单,按降序对相同值的字段重新排序。就像你有
(Field[0] = 0, Field[1] = 1, Field[2] = 0)然后结果应该是(Field[2], Field[0], Field[1]);简单! -
不,一点都不容易——“相同值的字段”到底是什么意思?如果你能给出一个简短但完整的例子来展示你的输入和期望的输出,那真的很有帮助。