如何对泛型列表中相同值的字段按降序排序。
示例:
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]);
很容易! - MahanGM