gpt4 book ai didi

sorting - 在自引用对象上覆盖compareTo(父/子关系)

转载 作者:行者123 更新时间:2023-12-02 15:02:50 25 4
gpt4 key购买 nike

我试图在grails的域类上实现compareTo,以便我可以返回SortedSet。我希望我的排序集按父级名称,然后按“子级”名称排序。例如(P = parent ,C = child ):

  • P-1
  • C-1
  • C-2
  • P-2
  • C-3
  • C-4

  • 我的课看起来像这样:
    class Issue implements Comparable {
    String name
    Issue parent

    @Override
    public int compareTo(obj){
    if(obj.parent!=null && this.parent!=null){
    parent.name.compareTo(obj.parent.name)
    }else{
    //What do I compare to sort the children relative to their parents?
    }
    }

    最佳答案

    如果您要查找的只是排序集,那么仅实现“可比较问题”并在映射上使用排序顺序就足够了吗?

    class Issue implements Comparable {
    String name
    Issue parent
    SortedSet children

    static hasMany = [children : Issue]
    static belongsTo = [parent : Issue]
    static mapping = {
    sort 'name'
    children sort:'name'
    }

    @Override
    public int compareTo(obj){
    if(obj){
    this.name?.compareTo(obj.name)
    }
    }

    关于sorting - 在自引用对象上覆盖compareTo(父/子关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7936242/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com