如何对釉面TreeList进行排序?

发布于 2024-12-02 07:13:05 字数 1114 浏览 6 评论 0原文

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

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

Root
  Node1
   Child1
   Child2
  Node2
   Child3

我只需要对 Node1 和 Node2 内部的子节点进行排序,分别对另一个子节点进行排序(这样只有 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 不能以正确的方式工作,但是,我看不到其他解决方案。或者可能是其他地方的问题,而不是我的比较器中的问题。

感谢您的耐心等待,我很高兴收到任何提示。 最好的问候,亚历克斯·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.

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

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

发布评论

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

评论(2

污味仙女 2024-12-09 07:13:05

当节点具有不同的父节点时,当前代码将返回 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
   }

Your current code returns 0 when to nodes have different parents. 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
   }
拥抱没勇气 2024-12-09 07:13:05

因此,这是我针对此问题的解决方案:DZone 文章。再说一次,它只是可能的解决方案之一,它并不完美,但它正在工作:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文