<div>Hi,</div>
<div>&nbsp;</div>
<div>you can just define a comparator that compares elements by their attribute value, like</div>
<div>&nbsp;</div>
<div>public class ElementAttributeComparator implements Comparator&lt;Element&gt; {</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; public int compare(Element e1, Element e2) {</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String href1 = e1.getAtttibuteValue(&quot;href&quot;);</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String href2 = e2.getAttributeValue(&quot;href&quot;);</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (href1 == null) {</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (href2 == null) return 0;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else return -1;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (href2 == null) return 1;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else return hef1.compareTo(href2);</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>}<br>(something may be wrong, writing from memory)</div>
<div>&nbsp;</div>
<div><br>and use it as parameter &#39;c&#39; in sortElments</div>
<div>&nbsp;</div>
<div>Best Regards,</div>
<div>Grzegorz</div>
<div>&nbsp;</div>
<div><span class="gmail_quote">2008/3/1, msook &lt;<a href="mailto:lmsook10@hotmail.com">lmsook10@hotmail.com</a>&gt;:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid"><br>Hello,<br>I&#39;m pretty new in JDOM world, and I&#39;ve got very excited project that I am<br>really interested in.<br>
<br>The xml file looks like<br>&lt;resources&gt;<br>&lt;resource type=&quot;webcontent&quot; identifier=&quot;acb-123&quot;<br>href=&quot;aaa/bbb263.xhtml&quot;&gt;<br>&lt;file href=&quot;aaa/bbb263.xhtml&quot; /&gt;<br>&lt;/resource&gt;<br>
&lt;resource type=&quot;webcontent&quot; identifier=&quot;sbs-345&quot;<br>href=&quot;aaa/ccc264.xhtml&quot;&gt;<br>&lt;file href=&quot;aaa/ccc264.xhtml&quot; /&gt;<br>&lt;/resource&gt;<br>&lt;resource type=&quot;quiz&quot; identifier=&quot;ccc-222&quot;<br>
href=&quot;aaa/bbb159.xml&quot;&gt;<br>&lt;file href=&quot;aaa/ccc159.xml&quot; /&gt;<br>&lt;/resource&gt;<br>&lt;resource type=&quot;quiz&quot; identifier=&quot;ddd-123&quot;<br>href=&quot;aaa/ddd170.xml&quot;&gt;<br>&lt;file href=&quot;aaa/ddd170.xml&quot; /&gt;<br>
&lt;/resource&gt;<br>&lt;/resources&gt;<br><br>And as you guess, there is a node called &quot;resources&quot; and under that node,<br>each resource&#39;s attribute href value is added(showing).<br>But I need to fix it to show that under &quot;resources&quot; node(parent), each<br>
resouce should be shown by href&#39;s alphabetical order.<br><br>I&#39;ve read the codes example regarding &quot;Sorting elements in a document<br>by a Node&quot; and this is it.<br><br>public static void sortElements(Element parent, Comparator c) {<br>
// Create a new, static list of child elements, and sort it.<br>List children = new ArrayList(parent.getChildren());<br>Collections.sort(children, c);<br>ListIterator childrenIter = children.listIterator();<br><br>// Create a new, static list of all content items.<br>
List content = new ArrayList(parent.getContent());<br>ListIterator contentIter = content.listIterator();<br><br>// Loop through the content items, and whenever we find an Element,<br>// we&#39;ll insert the next ordered Element in its place. Because the<br>
// content list is not live, it won&#39;t complain about an Element<br>being<br>// added twice.<br>while(contentIter.hasNext())<br>{<br>Object obj = contentIter.next();<br>if (obj instanceof Element)<br>contentIter.set(childrenIter.next());<br>
}<br><br>// Finally, we set the content list back into the parent Element.<br>parent.setContent(null);<br>parent.setContent(content);<br>}<br><br>But how can I sort elements by attribute?<br>Please share the code example if you have.<br>
<br>Thanks in advance.<br><br>--<br>View this message in context: <a href="http://www.nabble.com/How-can-I-sort-elements-by-an-attribute-value--tp15773258p15773258.html">http://www.nabble.com/How-can-I-sort-elements-by-an-attribute-value--tp15773258p15773258.html</a><br>
Sent from the JDOM - General mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br><br>_______________________________________________<br>To control your jdom-interest membership:<br><a href="http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com">http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>&quot;Choć tyle wiemy własnym doświadczeniem:<br>W nas jest Raj, Piekło - i do obu - szlaki.&quot;<br>J.K.