如何对glazed TreeList进行排序?(How to sort glazed TreeList?)

我有一个非常奇怪的问题 - 如何对glazed TreeList进行排序? 我在SWT NatTable中使用它,当我的数据提供程序设置为其中包含TreeList的GlazedListsDataProvider时,排序以一种非常奇怪的方式工作。 如果我使用GlazedListsDataProvider与SortedList,它工作正常。

例如,我的树看起来像这样:

Root Node1 Child1 Child2 Node2 Child3

我只需要将INSIDE节点1和节点2中的子节点分开,另外一个节点(这样只有child1和child2会改变它们的位置)。 但是,排序后看起来如下:

Root Node1 Node2 Child1 Child2 Child3

反向排序:

Root Node1 Node2 Child2 Child1 Child3

所以基本上,它有点工作(它确实以正确的方式对孩子进行排序),而且它对元素排序,它不应该排序。 这种行为可能是什么原因? 我的排序算法很简单:

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

我按照以下示例http://kari.dy.fi/src/sample/foldertree.zip中的建议进行排序 - 这意味着,在比较器构建在SortState之后,我将其设置为TreeList使用的TreeFormat。

我假设,返回0不能以正确的方式工作,但是,我看不到其他解决方案。 或者可能是其他地方的问题,而不是我的比较器。

感谢您的耐心等待,我将很高兴得到任何提示。 最好的问候,Alex G.

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. It works fine, if I am using GlazedListsDataProvider with 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). 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.

I assume, that returning 0 does not work in a correct way, however, I can not see other solution. 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.

最满意答案

当节点具有不同的父节点时,您当前的代码返回0 。 就像是,'如果他们有不同的父母,我不在乎哪一个先行'。 但我认为你想要,'如果他们有不同的父母,第一个应该是第一个父母的那个'。 如果您只想在父母中进行自定义排序,您应该按照原样在父母之外进行排序。 不确定具体的代码,但你可以这样做:

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 }

要么

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. Once again, it is only one of the possible solutions, and it is not perfect, but it is working:)

更多推荐