[jdom-interest] Problems with Collections.sort(List,Comparator)

Marcel Heuzer marcel.heuzer at sagelex.be
Wed Apr 10 04:19:54 PDT 2002


When I try to sort a list of children with this code:

  public Element getListSortedByString(Element element) {
    try {
      List list = element.getChildren();
      Collections.sort(list, new ContentTagComparator());
    catch(Exception e) {
    return element;


  public class ContentTagComparator implements Comparator {
    public int compare(Object o1,Object o2) {

      String s1 = ((Element)o1).getText();
      String s2 = ((Element)o2).getText();
      int result = s1.compareTo(s2);
      return result;


I get this exception at the end of Collections.sort(list, new ContentTagComparator()); process:

org.jdom.IllegalAddException: The element already has an existing parent
 at org.jdom.ContentList.add(ContentList.java:190)

 at org.jdom.ContentList.add(ContentList.java:146)

 at org.jdom.ContentList.set(ContentList.java:663)

 at org.jdom.ContentList$FilterListIterator.set(ContentList.java:1150)

 at java.util.Collections.sort(Unknown Source)

Is it a bug ?
Is there another way to sort elements content ?


Marcel Heuzer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20020410/1e7db1d1/attachment.htm

More information about the jdom-interest mailing list