How can I sort same value fields in generic lists with a descending order.
Example:
List<int> objList = new List<int>();
objList.Add(1);
-> objList.Add(0);
-> objList.Add(0);
objList.Add(2);
-> objList.Add(0);
It’s my source somehow and I want to sort for example zero values in descending mode.
I use this code for sorting the numbers (actually the depths), and above example is not related to this but somehow it’s same. In my generic list I have several depths which they might be same to each other and I want to order the same fields descending.
Objects.Sort(
delegate(Classes.Object.GameObject Object1, Classes.Object.GameObject Object2)
{
return Object1.Depth.CompareTo(Object2.Depth);
}
);
Answer: Might help someone in the future
// 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();
}
If I understand you correctly, you want your list sorted in descending order based on
GameObject.Depth, and you’ve got an implementation that sorts your collection, but in ascending order rather than descending. Given that, here’s the laziest answer I could come up with:Code edited per my comment. Really, why couldn’t you have said what you wanted in the question? I agree that it isn’t complicated, but you won’t get good help if you don’t ask good questions.
Hardly optimal, but it’s not meant to be.