I am trying to use IComprarer to a complex sort. I have this complex type (for sake of simplicity, three ints):
ID | A | B
1 | 1 | 10
2 | 3 | 20
3 | 1 | 30
4 | 3 | 5
5 | 2 | 15
And i need to Sort by B, but i need to keep A together. B can be ASC or DESC but I can not separate 'A'
It must order this way:
ID | A | B
3 | 1 | 30
1 | 1 | 10
2 | 3 | 20
4 | 3 | 5
5 | 2 | 15
There are NOTHING special in A order, the important things are: B is ordered and the same A must not be separated.
I tried:
public int Compare(Comanda x, Comanda y)
{
if (x.A == y.A) return 0;
return x.B.CompareTo(y.B);
}
But does not work and I even don't know HOW to ask it to google, "group" keyword do not help much.
And also tried to sort twice which, obviously doesn't work (discarded the B order).
list.Sort(new SortByB());
list.Sort(new SortByA());
How can I do it ? Thanks
This is not a duplicate of "C# List<> Sort by x then y" bucause i don't want do double-sort. I wanted to GROUP