I have a List object that contains items in following type
public class Item{
public int Type; //this can be only 0 or 1
public string Text;
}
I would like to sort the entire list by the Type first and then do another sort by Text for only items that have type is 1?
How to achieve this?
Many thanks
Ming
Here’s how I’d do it.
This is assuming there are no empty Text strings where i == 0. If you can’t make that assumption, the following would work:
By giving all items with a 0 Type the same value, you’ll leave them all in their initial order, while sorting other items by their Text value.
On a side note, this is a code smell:
At a glance, that sounds like it ought to be a boolean or a custom
enumtype. After taking a deeper look at what you’re trying to do, it sounds like maybe you’re not expectingTextto even be a legitimate value if Type is 0. If that’s the case, you should consider using class inheritance to actually differentiate one item type from another.