Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

[jdom-interest] Sorting children

Thomas Scheffler

2006-04-06


Hi,

I was searching for help on sorting childrens of a Element. As you propably allready noticed you cannot simply use Collecetions.sort(List, Comparator). I was investigating the issue and some of it's solutions. All of them need to copy the children List, which didn't sound good to me on large scale batch processes. I would really like to see some kind of support for this on the next JDOM release. For the moment I would like to contribute my current solution and would like to hear your suggestions on that.

void sort(List list, Comparator c) {

Element[] a = (Element[]) list.toArray(new Element[list.size()]);

Arrays.sort(a, c);

for (int i = 0; i < a.length; i++) {

a[i].detach(); //removes Element also from "list"

}

for (int i = 0; i < a.length; i++) {

list.add(a[i]); //readd Element to "list" in the right order

}

}

regards

Thomas

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.