<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=us-ascii" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>It's
because the items returned by getChildren...i.e. Element, Comment etc...do not
implement the Comparable interface that the sort method
uses.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>Also,
because the getChildren returns a list of objects that are NOT the same, it
means that doing a sort on these objects makes no sense. You need to pull
out the actual information you want, probably in this case the text within the
elements named "menuitem" or whatever, put those into an array or Collection
container, like ArrayList and then sort...the String class does implement the
Comparable interface so sorting is possible.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>So
maybe change your code to say:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>// Get
all the children that are menuitems...</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>List
menuitems = menuitems.getChildren ("menuitem");</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>//
Create a new ArrayList to hold the items, remember that ArrayList is NOT thread
safe.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001>ArrayList items = new ArrayList ();</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001>ListIterator menuitemsit = menuitems.listIterator
();</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>//
Cycle over all the children for menu items...</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>while
(menuitemsit.hasNext ())</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001>{</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001> // Get the element which represents
the menu item...this could also be an attribute etc...</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001> Element menitem = (Element)
menuitemsit.next ();</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001> // Get the text of the menu
item..</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001> items.add (menitem.getTextTrim
());</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001>}</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=039130313-16032001>//
Sort the list...</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001>Collections.sort (items);</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=039130313-16032001>G.</SPAN></FONT></DIV>
<BLOCKQUOTE>
<DIV align=left class=OutlookMessageHeader dir=ltr><FONT face=Tahoma
size=2>-----Original Message-----<BR><B>From:</B> jdom-interest-admin@jdom.org
[mailto:jdom-interest-admin@jdom.org]<B>On Behalf Of </B>Bruce
Altner<BR><B>Sent:</B> Friday, March 16, 2001 12:42 PM<BR><B>To:</B>
jdom-interest@jdom.org<BR><B>Subject:</B> [jdom-interest] Sorting a list of
text elements<BR><BR></DIV></FONT>Greetings:<BR><BR>I am trying to sort a list
of text elements within my servlet, which uses JDOM, for a pull down menu and
am not getting it to work. Maybe I'm using the sort method (part of the Java 2
Collections framework) incorrectly. Here is the code
snippet:<BR><BR> //get root and all first level children (as
List)<BR> Element root =
doc.getRootElement();<BR> Element menuItems =
root.getChild("Categories");<BR> List menuItem =
menuItems.getChildren();<BR>
<B>Collections.sort(menuItem);<BR><BR></B>
out.println("<X-TAB> </X-TAB><td><SELECT
NAME=\"category\">");
<DL>
<DD> for(int i=0;i<menuItem.size();i++){
<DL>
<DD> Element elem = (Element)menuItem.get(i);
<DD> String menuItemText = elem.getTextTrim();
<DD>
out.println("<OPTION>"+menuItemText+"</OPTION>"); </DD></DL>
<DD> } </DD></DL>
out.println("<X-TAB> </X-TAB></SELECT>");<BR><BR>The
Collections.sort(menuItem) line has no effect: the list is not sorted. I am
expecting alphabetical sorting as the "natural order." Can someone point out
what I need to do?<BR><BR>Thanks, in advance,<BR>Bruce
</BLOCKQUOTE></BODY></HTML>