.NET 中的自定义排序

发布于 2024-09-12 10:26:51 字数 595 浏览 4 评论 0原文

我为父子关系设计了一个类,

  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”个类别

  1. 这里我需要获取顶级父级,其中包含按 CatName 排序的所有子类别。有什么想法如何实现这一点?
  2. 我的班级设计好吗?

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

  1. Here I need to get the Top Parent with all the child categories sorted by CatName. Any ideas How this can be achieved?
  2. Is my class design GOOD?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰沐子 2024-09-19 10:26:51

你不能,因为你没有对父级的引用。你必须添加一个字段:

public Category Parent { get; set; }

并修改add方法来设置父项:

public void addChildCat(Category childCat)
{
  childCat.Parent = this;
  this.childCategory.Add(childCat);
}

你需要父项来获取根:

public static Category SortedCategory(Category cat)
{
  // get the root
  var root = cat;
  while(root.Parent != null) root = root.Parent;

  return root.GetSorted();
}

private Category GetSorted()
{
  var sortedChildren = new List<Category>(childCategories).ConvertAll(c => c.GetSorted());
  sortedChildren.Sort((c1, c2) => c1.CatName.CompareTo(c2.Catname));

  return new Category { CatName = root.CatName, 
                        Catid = root.CatId,
                        childCategories = sortedChildren; }
}

You can't because you have not a reference to the parent. You have to add a field:

public Category Parent { get; set; }

and modify the add method to set the parent:

public void addChildCat(Category childCat)
{
  childCat.Parent = this;
  this.childCategory.Add(childCat);
}

You need the parent to get the root:

public static Category SortedCategory(Category cat)
{
  // get the root
  var root = cat;
  while(root.Parent != null) root = root.Parent;

  return root.GetSorted();
}

private Category GetSorted()
{
  var sortedChildren = new List<Category>(childCategories).ConvertAll(c => c.GetSorted());
  sortedChildren.Sort((c1, c2) => c1.CatName.CompareTo(c2.Catname));

  return new Category { CatName = root.CatName, 
                        Catid = root.CatId,
                        childCategories = sortedChildren; }
}
遗弃M 2024-09-19 10:26:51

您可以使用 SortedList 来跟踪相反,子类别。

You can use the SortedList to keep track of the child categories instead.

往昔成烟 2024-09-19 10:26:51

如果我理解了这一点,我们就有了一个树结构,对吧?您期望的结果是什么,最顶层父级(根)的排序子级?

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)?

舞袖。长 2024-09-19 10:26:51

而不是:

public string CatName;
public string CatId;

我会这样做:

 class Cat
 {
      public string Name { get; set; }
      public string Id { get; set; }
 }

而不是:

public Category SortedCategory(Category cat)
    {
      // Should return the sorted cat i.e topmost parent        
    }

我会这样做:

 var category = new List<Cat>
                               {
                                   new Cat() {Name = "cat1", Id = "123"},
                                   new Cat() {Name = "cat2", Id = "124"},
                                   new Cat() {Name = "cat3", Id = "125"},
                                   new Cat() {Name = "cat4", Id = "126"}
                               };


category.Sort(( cat1, cat2) =>  ((Convert.ToInt32(cat1.Id) > Convert.ToInt32(cat2.Id)) ? 1 : 0) );

Instead of :

public string CatName;
public string CatId;

I would do:

 class Cat
 {
      public string Name { get; set; }
      public string Id { get; set; }
 }

And instead of:

public Category SortedCategory(Category cat)
    {
      // Should return the sorted cat i.e topmost parent        
    }

I would do:

 var category = new List<Cat>
                               {
                                   new Cat() {Name = "cat1", Id = "123"},
                                   new Cat() {Name = "cat2", Id = "124"},
                                   new Cat() {Name = "cat3", Id = "125"},
                                   new Cat() {Name = "cat4", Id = "126"}
                               };


category.Sort(( cat1, cat2) =>  ((Convert.ToInt32(cat1.Id) > Convert.ToInt32(cat2.Id)) ? 1 : 0) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文