« Return to Thread: Sorting TreeList

Re: Sorting TreeList

by Ken Orr :: Rate this Message:

Reply to Author | View in Thread

The comparator that you supply to your TreeFormat is used to group items together. That comparator should return "equal" if two items belong to the same parent.

Here's how you might setup your list pipeline without a sorted list (I'll reference what the output would be if you used my example here):

BasicEventList -> ThreadSafeList -> SwingThreadProxyList -> TreeList

+ Apple.txt
    - skin
    - stem
    - flesh
+ Frogs.txt
    - eyes
    - legs
    - sounds
    - feet

Note that the grouping nodes (Apple.txt and Frogs.txt) are in sorted order, but the "name" child items are not.

If we want to sort the elements within the TreeList, we can insert a SortedList that sorts the items ahead of time, and then feed that to the TreeList:

BasicEventList -> ThreadSafeList -> SortedList -> SwingThreadProxyList -> TreeList

Results in:

- flesh, Apple.txt
- skin, Apple.txt
- stem, Apple.txt
- eyes, Frogs.txt
- feet, Frogs.txt
- legs, Frogs.txt
- sounds, Frogs.txt

which is then turned into the following by TreeList:

+ Apple.txt
    - flesh
    - skin
    - stem
+ Frogs.txt
    - eyes
    - feet
    - legs
    - sounds

So the comparator to your SortedList would use sort on the name property whereas the TreeList was comparing against Files (in the strange example I provided).

As far as how to chain you lists, I try to do it in a computationally aware way. That is, filter before you sort, as it makes the sort operation simpler, though the GL guys may tell you this isn't necessary.

Good luck,
-Ken


Thanks for the quick reply!

With regards to the sort comparator used for the sorted list, would I use the same comparator *TreeFormat (in your example SimpleBeanTreeFormat)?

Also, if the chain of events is:
BasicEventList -> SortedList -> ObservableElementList -> TreeList

At which level would I apply the GlazedListsSwing.swingThreadProxyList() and the GlazedLists.threadSafeList()?  Should I be applying these at the lowest level (i.e. BasicEventList), or at the highest level (i.e TreeList)?

And one more point with regards to chaining Lists together, is there a standard way that one list must be chained to the next? Or is it a free-for-all in the order that you chain lists together?


Cheers!


Ken Orr wrote:
If you add a SortedList upstream of your TreeList, you'll get sorted output.

SortedList -> TreeList

dingwa wrote:
Hi,

How is it possible to sort TreeLists?  When I add rows to the tree list they seem be be inserted into an arbitrary position.  I am wanting to control the position of any new rows that are inserted into the TreeList (i.e. at the top of the table)

Would I have either have to insert the row into the TreeList and then sort the list so the newly inserted row is at the top? Or is it even possible to insert the top level expandable row at a specified position?


Cheers!

 « Return to Thread: Sorting TreeList