[jdom-interest] Problems by sorting the list (JDOM beginner)

PJ Fanning pj.fanning at XIAM.com
Thu Mar 28 01:46:46 PST 2002


Oops. That won't work at all. The list will be emptied.
Try this:
List live = root.getChildren;
ArrayList notLive = new ArrayList(live);
Collections.sort(notLive , mycomp);

Any modifications (eg sorting) of the 'live' list will result in changes to
the underlying document structure. The 'notLive' list can be sorted without
affecting the underlying document structure (and therefore there should be
no risk of a ConcurrentModificationException).

-----Original Message-----
From: PJ Fanning 
Sent: 28 March 2002 09:39
To: 'jdom-interest at jdom.org'
Cc: 'sabela_julien_grouteau at yahoo.fr'; 'galuc at ebruit.com'
Subject: RE: [jdom-interest] Problems by sorting the list (JDOM
beginner)


I would suggest the following.

List ch = root.getChildren;
Iterator it = ch.iterator ();
while (it.hasNext ()) {
	Object obj = it.next();
	it.remove();
}
Collections.sort (ch , mycomp);

The it.remove() will automatically update the root's list of children and
detach the children.

---Original Message---
One thing I found useful, while sorting list of childrens, is to detach them
prior to any 'processing' on them.

So I would say :

List ch = root.getChildren;
Iterator it = ch.iterator ();
while (it.hasNext ()) {
	Element element = (Element) it.next ();
	element.detach ();
}
Collections.sort (ch , mycomp);

Hope this helps.

Julien



More information about the jdom-interest mailing list