sorting a treelist

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

sorting a treelist

Jason S
Could someone comment on this old thread:
http://glazedlists.1045722.n5.nabble.com/Sorting-TreeList-td3419834.html#none

I see what looks like some erroneous statements, namely that if you sort upstream from a TreeList, the treelist will be sorted (doesn't seem to work for me), and that the comparator in the TreeList.Format is used to group items together (how can this be possible? you're comparing items, not parents of items).

What are the requirements of the TreeList.Format comparator?

e.g. if these are my TreeList nodes

- A
-- A1
-- A2
- B
-- B1
-- B2
-- B3
--- B3a
--- B3b
- C

then:
- which nodes are going to be compared to each other? is it comparisons within the sets (A,B,C) and (A1,A2) and (B1,B2,B3) and (B3a,B3b) or is it all 10 nodes, or something else?
- what happens if I return null for a comparator? (bad things seem to happen as some nodes seem to be duplicated in the output list)

Also is it possible to change the comparators at runtime, in the way that TableComparatorChooser operates?
Reply | Threaded
Open this post in threaded view
|

re: sorting a treelist

Kevin Day
Awhile back, I started a refactoring effort to make this sort of things work.  I split out the ability to adjust comparators into a separate interface like this:
 
/*
 * Created on Feb 2, 2010
 * (c) 2010 Trumpet, Inc.
 *
 */
package ca.odell.glazedlists.gui;
import java.util.Comparator;
import ca.odell.glazedlists.util.concurrent.ReadWriteLock;
/**
 * Indicates that the implmenter is based on a comparator that can be swapped out
 */
public interface ComparatorAdjustable<E> {
    public Comparator<? super E> getComparator();
    public void setComparator(Comparator<? super E> comparator);
    public ReadWriteLock getReadWriteLock();
}
 
 
I rewrote SortedList to take advantage of that, then started implementing TreeList along the same lines.  Getting the sorters to change in tree list wasn't much of a problem (the sort happened before the tree organization.  Unfortunately, I ran into a number of problems with the lack of robustness (and performance) of TreeList, and wound up abandonding the effort due to required ship date for our product...  If I recall, the biggest issue was that the treelist would wind up having duplicate phantom nodes in it.
 
Sorry I couldn't give you a total solution, but if sorting the treelist is important (and your trees aren't massive), the above may help you get started.
 
- K
 
----------------------- Original Message -----------------------
  
From: Jason S [hidden email]
Cc: 
Date: Tue, 16 Aug 2011 07:15:17 -0700 (PDT)
Subject: sorting a treelist
  
Could someone comment on this old thread:
http://glazedlists.1045722.n5.nabble.com/Sorting-TreeList-td3419834.html#none

I see what looks like some erroneous statements, namely that if you sort
upstream from a TreeList, the treelist will be sorted (doesn't seem to work
for me), and that the comparator in the TreeList.Format is used to group
items together (how can this be possible? you're comparing items, not
parents of items).

What are the requirements of the TreeList.Format comparator?

e.g. if these are my TreeList nodes

- A
-- A1
-- A2
- B
-- B1
-- B2
-- B3
--- B3a
--- B3b
- C

then:
- which nodes are going to be compared to each other? is it comparisons
within the sets (A,B,C) and (A1,A2) and (B1,B2,B3) and (B3a,B3b) or is it
all 10 nodes, or something else?
- what happens if I return null for a comparator? (bad things seem to happen
as some nodes seem to be duplicated in the output list)

Also is it possible to change the comparators at runtime, in the way that
TableComparatorChooser operates?

--
View this message in context: http://glazedlists.1045722.n5.nabble.com/sorting-a-treelist-tp4704550p4704550.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: sorting a treelist

Jason S
In reply to this post by Jason S
Jason S wrote
Also is it possible to change the comparators at runtime, in the way that TableComparatorChooser operates?
Argh, I'm looking at the TreeList code now + it looks iffy :/

- setTreeFormat does nothing (see http://java.net/jira/browse/GLAZEDLISTS-521 )
- if you keep the same TreeFormat object, but now use a different comparator, there doesn't seem to be a way to cause TreeList to re-sort its nodes
Reply | Threaded
Open this post in threaded view
|

Re: sorting a treelist

Jason S
Is there any way to add a filter into the pipeline which can create a fake "update all" event, so that downstream lists have to re-create themselves?
Reply | Threaded
Open this post in threaded view
|

re[2]: sorting a treelist

Kevin Day
Yes - just iterate the list and do a set() operation on each item.
 
       list.getReadWriteLock().writeLock().lock();
       try{
           for(int i = list.size()-1; i >= 0; i--){
               list.set(i, list.get(i));
           }
       } finally {
           list.getReadWriteLock().writeLock().unlock();
       }
 
(no particular reason to walk the list backwards)
 
- K
 
----------------------- Original Message -----------------------
  
From: Jason S [hidden email]
Cc: 
Date: Tue, 16 Aug 2011 09:11:55 -0700 (PDT)
Subject: Re: sorting a treelist
  
Is there any way to add a filter into the pipeline which can create a fake
"update all" event, so that downstream lists have to re-create themselves?

--
View this message in context: http://glazedlists.1045722.n5.nabble.com/sorting-a-treelist-tp4704550p4705008.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: re[2]: sorting a treelist

Jason S
Kevin Day wrote
Yes - just iterate the list and do a set() operation on each item.
Hey, that worked -- thanks!

OK, I've ended up creating a dummy SortedList of the treelist items, that I can then install TableComparatorChooser on, and I add a ListEventListener to the dummy sorted list; when I see changes, I check to see if the SortedList comparator has changed, and if it does, then I propagate the new comparator to the TreeFormat object and update the list by listiterator()+set() as you suggest.