I have a class named move
it has a field named val of type int
And I have a list of moves named mlist
I wrote this
mlist.Sort((b, a) =>
{
if (a.val > b.val)
return 1;
else if (a.val == b.val)
return 0;
else
return -1;
}
);
at first my list have two objects that both has .val = 0
when I sort the list the place of these two object is changed. But I don’t want to this to happen. What can I do?
Sort performs a quicksort which is an unstable sort.
If you want a stable sort you could use the
OrderBymethod which is available through LinQUsing
OrderByin your example would be assuming thatmListis aList<>Should you desire the reverse ordering, the method is called
OrderByDescending