[jdom-interest] Sorting JDOM lists

Chris B. chris at tech.com.au
Mon Mar 8 01:49:52 PST 2004


Jason Hunter wrote:

> Elements have parentage and can only have one parent at a time.  
> Sorting in place can require an element to have two parents for a time.


Sorting could never change the parent. It just tries to add it in the 
array at a different location.

> The easy solution is to detach the elements, then reinsert in sorted 
> order.
>
> List l = elt.removeContent();
> sort(l);
> elt.addContent(l);
>
> Feel free to tell us now how we should have done this differently.  :-)


Well... why throw the error when the parent hasn't changed?

>
> -jh-
>
> Chris B. wrote:
>
>>
>> Is there any reason why JDOM won't allow you to sort a list of 
>> Elements??
>>
>> Element el...
>> java.util.List items = (java.util.List)el.getChildren("aaaa", ns);
>> Collections.sort(headeritems, new Comparator() {
>>    ......
>> });
>>
>>
>> org.jdom.IllegalAddException: The element already has an existing 
>> parent "email:headers"
>>    at org.jdom.ContentList.add(ContentList.java:177)
>>    at org.jdom.ContentList.add(ContentList.java:138)
>>    at org.jdom.ContentList.set(ContentList.java:679)
>>    at org.jdom.ContentList$FilterListIterator.set(ContentList.java:1142)
>>    at java.util.Collections.sort(Collections.java:159)
>>
>>
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com 
>>
>>



More information about the jdom-interest mailing list