I have designed a Class for Parent Child relationship
class Category
{
public string CatName;
public string CatId;
public IList<Category> childCategory = new List<Category>();
public void addChildCat(Category childCat)
{
this.childCategory.Add(childCat);
}
public Category SortedCategory(Category cat)
{
// Should return the sorted cat i.e topmost parent
}
}
Here by Parent will not have Catname or CatId, it will have Child Categories which has CatName, CatId as well as another Child Category List and it goes till “N” categories
- Here I need to get the Top Parent with all the child categories sorted by CatName. Any ideas How this can be achieved?
- Is my class design GOOD?
You can’t because you have not a reference to the parent. You have to add a field:
and modify the add method to set the parent:
You need the parent to get the root: