I have the following simplified classes:
public class BaseContainer
{
public BaseContainer()
{
Children = new ObservableCollection<BaseContainer>();
}
public ObservableCollection<BaseContainer> Children { get; set; }
}
public class ItemA : BaseContainer
{
public ItemA()
{
base.Children.Add(new ItemB() { ItemBName = "bb" });
base.Children.Add(new ItemA() { ItemAName = "ab" });
base.Children.Add(new ItemB() { ItemBName = "ba" });
base.Children.Add(new ItemA() { ItemAName = "aa" });
}
public string ItemAName { get; set; }
}
public class ItemB : BaseContainer
{
public string ItemBName { get; set; }
}
I’m trying to sort my ItemA.Children collection based on two conditions:
- All Item A’s must come first in the collection
- Item A’s should be sorted by ItemAName
- Item B’s should be sorted by ItemBName
So after sorting I’d expect something like this:
- ItemA – ItemAName = “aa”
- ItemA – ItemAName = “ab”
- ItemB – ItemBName = “ba”
- ItemB – ItemBName = “bb”
Any ideas on how to accomplish this?
I was able to sort by class type name:
List<BaseContainer> temp = base.Children.ToList();
temp.Sort((x, y) => string.Compare(x.GetType().Name, y.GetType().Name));
base.Children.Clear();
base.Children.AddRange(temp);
But Names are not sorted…
Thanks!
This will get all the A’s, order them by AName, then concat with all the B’s ordered by B name.
I’m not sure this is the design I would go with… you might consider adding something to the base class to facilitate this kind of sorting.