I have a class that has 3 properties: Name, ID, and ParentID.
My data:
Name ID ParentID
Event A 1 1
Event B 2 1
Event C 3 1
Event D 4 2
I have everything in a List and was trying to use the OrderBy or perhaps the Sort methods. Not sure which would be better.
I need the data in the list to be ordered so that an event has it’s child as the next item in the list. Any help on this would be greatly appreciated, I am doing this in VB by the way. Thanks!!
You can sort the list like this
Explanation: I am using a lambda expression here. You can think of it as a kind of inline declaration of a function.
CompareToreturns either-1,0or+1. A negative number meansxis less thany,0both are equal and+1meansxis greater thany. By multiplying the first comparison by two, its sign takes precedence over the second comparison. The second has only an effect, if the first one returns0.The advantage of using the lists
Sortmethod over LINQ is that the list is sorted in-place. With LINQ you would have to create a new list.