[jdom-interest] Sorting JDOM lists

Jason Hunter jhunter at xquery.com
Mon Mar 8 00:56:16 PST 2004


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

-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