简体   繁体   English

如何对玻璃树列表进行排序?

[英]How to sort glazed TreeList?

I have a pretty weird question - how to sort glazed TreeList? 我有一个很奇怪的问题-如何对玻璃树列表进行排序? I am using it in SWT NatTable, and when my data provider is set to GlazedListsDataProvider with TreeList inside it, sorting works in a very strange way. 我在SWT NatTable中使用它,并且当我的数据提供程序设置为GlazedListsDataProvider且其中包含TreeList时,排序的方式非常奇怪。 It works fine, if I am using GlazedListsDataProvider with SortedList. 如果我将GlazedListsDataProvider与SortedList结合使用,则效果很好。

For instance, my tree looks like that: 例如,我的树看起来像这样:

Root
  Node1
   Child1
   Child2
  Node2
   Child3

I need to sort only children INSIDE Node1 and Node2, separately one of another (so that only child1 and child2 will change their place). 我只需要排序子节点INSIDE 1和Node2中的另一个子节点(以便仅child1和child2可以更改其位置)。 However, After sorting it looks like following: 但是,排序后看起来如下:

Root
  Node1
  Node2
   Child1
   Child2
   Child3

Reversed sorting: 反向排序:

Root
  Node1
  Node2
   Child2
   Child1
   Child3

So basically, it kind of working (it does sort children in a correct way), but moreover it sorts elements, which it should not sort. 因此,基本上,它是可以工作的(它确实以正确的方式对子级进行了排序),但是它对元素进行了排序,而不应该对其进行排序。 What could be a reason of such behavior? 这种行为可能是什么原因? My sorting algorithm is simple: 我的排序算法很简单:

compare (element1, element2) {
   if (both elements are under same parent and have same type)
     compare
   otherwise
     return 0
   }

I am doing sorting as proposed in following example http://kari.dy.fi/src/sample/foldertree.zip - meaning, that after comparator is build in SortState I am setting it into the TreeFormat, used by TreeList. 我正在按照以下示例中的建议进行排序: http ://kari.dy.fi/src/sample/foldertree.zip-意思是,在SortState中构建比较器之后,我将其设置为TreeList使用的TreeFormat。

I assume, that returning 0 does not work in a correct way, however, I can not see other solution. 我假设返回0不能以正确的方式工作,但是,我看不到其他解决方案。 Or may be it is a problem somewhere else, not in my comparator. 也许这是其他地方的问题,不在我的比较器中。

Thank you for your patience, I will be glad to get any hints. 多谢您的耐心配合,我们将很高兴收到任何提示。 Best regards, Alex G. 最好的问候,Alex G.

Your current code returns 0 when to nodes have different parents. 当到具有不同父节点的节点时,当前代码将返回0 That's like, 'if they have different parents, I don't care which one goes first'. 就像,“如果他们有不同的父母,我不在乎哪一个先行”。 But I think you want, 'if they have different parents, the first should be the one with the first parent'. 但是我想您要,“如果他们有不同的父母,那么第一个应该是有第一个父母的那个”。 If you want to do custom sorting inside the parents only, you should keep sorting outside the parents the way it was. 如果只想在父母内部进行自定义排序,则应保持在父母外部进行排序。 Not sure about the exact code, but you could do something like: 不确定确切的代码,但是您可以执行以下操作:

compare (element1, element2) {
   if (both elements are under same parent and have same type)
     compare
   otherwise
     return original.compare(element1,element2)//delegate to the original sorting
   }

Or 要么

compare (element1, element2) {
   if (both elements are under same parent and have same type)
     compare
   otherwise
     compare(element1.parent,element2.parent) // sort on parent level
   }

So, here is my solution for this problem: DZone Article . 因此,这是我针对此问题的解决方案: DZone文章 Once again, it is only one of the possible solutions, and it is not perfect, but it is working:) 再一次,它只是可能的解决方案之一,虽然不是完美的,但是它正在起作用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM