.NET 中的自定义排序
我为父子关系设计了一个类,
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
}
}
这里的父级不会有 Catname 或 CatId,它将有子类别,其中有 CatName、CatId 以及另一个子类别列表,直到“N”个类别
- 这里我需要获取顶级父级,其中包含按 CatName 排序的所有子类别。有什么想法如何实现这一点?
- 我的班级设计好吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能,因为你没有对父级的引用。你必须添加一个字段:
并修改add方法来设置父项:
你需要父项来获取根:
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:
您可以使用 SortedList 来跟踪相反,子类别。
You can use the SortedList to keep track of the child categories instead.
如果我理解了这一点,我们就有了一个树结构,对吧?您期望的结果是什么,最顶层父级(根)的排序子级?
If I understand this, we have a tree structure right? And what is the result you are expecting, the sorted children of the topmost parent (root)?
而不是:
我会这样做:
而不是:
我会这样做:
Instead of :
I would do:
And instead of:
I would do: