重写自引用对象上的compareTo(父/子关系)
我正在尝试在 grails 中的域类上实现compareTo,以便我可以返回SortedSet。我希望我的排序集按父名称排序,然后按“子”名称排序。例如(P=父级,C=子级):
- 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?
}
}
I'm trying to implement compareTo on a domain class in grails so I can return a SortedSet. I want my sorted set to be ordered by parent name, then by "child" name. For example (P=parent, C=child):
- P-1
- C-1
- C-2
- P-2
- C-3
- C-4
My class looks something like this:
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?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要寻找的只是排序集,那么仅在问题上实现 Comparable 并在映射上使用排序顺序就足够了吗?
If all you're looking for is sorted sets, would just implementing Comparable on Issue and using sort orders on the mappings suffice?