<div>Hi,</div>
<div> </div>
<div>you can just define a comparator that compares elements by their attribute value, like</div>
<div> </div>
<div>public class ElementAttributeComparator implements Comparator<Element> {</div>
<div> </div>
<div> public int compare(Element e1, Element e2) {</div>
<div> </div>
<div> String href1 = e1.getAtttibuteValue("href");</div>
<div> String href2 = e2.getAttributeValue("href");</div>
<div> </div>
<div> if (href1 == null) {</div>
<div> if (href2 == null) return 0;</div>
<div> else return -1;</div>
<div> }</div>
<div> else {</div>
<div> if (href2 == null) return 1;</div>
<div> else return hef1.compareTo(href2);</div>
<div> }</div>
<div> </div>
<div> }</div>
<div> </div>
<div>}<br>(something may be wrong, writing from memory)</div>
<div> </div>
<div><br>and use it as parameter 'c' in sortElments</div>
<div> </div>
<div>Best Regards,</div>
<div>Grzegorz</div>
<div> </div>
<div><span class="gmail_quote">2008/3/1, msook <<a href="mailto:lmsook10@hotmail.com">lmsook10@hotmail.com</a>>:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid"><br>Hello,<br>I'm pretty new in JDOM world, and I've got very excited project that I am<br>really interested in.<br>
<br>The xml file looks like<br><resources><br><resource type="webcontent" identifier="acb-123"<br>href="aaa/bbb263.xhtml"><br><file href="aaa/bbb263.xhtml" /><br></resource><br>
<resource type="webcontent" identifier="sbs-345"<br>href="aaa/ccc264.xhtml"><br><file href="aaa/ccc264.xhtml" /><br></resource><br><resource type="quiz" identifier="ccc-222"<br>
href="aaa/bbb159.xml"><br><file href="aaa/ccc159.xml" /><br></resource><br><resource type="quiz" identifier="ddd-123"<br>href="aaa/ddd170.xml"><br><file href="aaa/ddd170.xml" /><br>
</resource><br></resources><br><br>And as you guess, there is a node called "resources" and under that node,<br>each resource's attribute href value is added(showing).<br>But I need to fix it to show that under "resources" node(parent), each<br>
resouce should be shown by href's alphabetical order.<br><br>I've read the codes example regarding "Sorting elements in a document<br>by a Node" 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'll insert the next ordered Element in its place. Because the<br>
// content list is not live, it won'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>"Choć tyle wiemy własnym doświadczeniem:<br>W nas jest Raj, Piekło - i do obu - szlaki."<br>J.K.