Re: How can I sort elements by an attribute value?

"Grzegorz Kaczor" <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
Hi,

you can just define a comparator that compares elements by their attribute
value, like

public class ElementAttributeComparator implements Comparator<Element> {

    public int compare(Element e1, Element e2) {

        String href1 = e1.getAtttibuteValue("href");
        String href2 = e2.getAttributeValue("href");

        if (href1 == null) {
            if (href2 == null) return 0;
            else return -1;
        }
        else {
             if (href2 == null) return 1;
             else return hef1.compareTo(href2);
        }

    }

}
(something may be wrong, writing from memory)


and use it as parameter 'c' in sortElments

Best Regards,
Grzegorz

2008/3/1, msook <[email protected]>:
>
>
> Hello,
> I'm pretty new in JDOM world, and I've got very excited project that I am
> really interested in.
>
> The xml file looks like
> <resources>
> <resource type="webcontent" identifier="acb-123"
> href="aaa/bbb263.xhtml">
> <file href="aaa/bbb263.xhtml" />
> </resource>
> <resource type="webcontent" identifier="sbs-345"
> href="aaa/ccc264.xhtml">
> <file href="aaa/ccc264.xhtml" />
> </resource>
> <resource type="quiz" identifier="ccc-222"
> href="aaa/bbb159.xml">
> <file href="aaa/ccc159.xml" />
> </resource>
> <resource type="quiz" identifier="ddd-123"
> href="aaa/ddd170.xml">
> <file href="aaa/ddd170.xml" />
> </resource>
> </resources>
>
> And as you guess, there is a node called "resources" and under that node,
> each resource's attribute href value is added(showing).
> But I need to fix it to show that under "resources" node(parent), each
> resouce should be shown by href's alphabetical order.
>
> I've read the codes example regarding "Sorting elements in a document
> by a Node" and this is it.
>
> public static void sortElements(Element parent, Comparator c) {
> // Create a new, static list of child elements, and sort it.
> List children = new ArrayList(parent.getChildren());
> Collections.sort(children, c);
> ListIterator childrenIter = children.listIterator();
>
> // Create a new, static list of all content items.
> List content = new ArrayList(parent.getContent());
> ListIterator contentIter = content.listIterator();
>
> // Loop through the content items, and whenever we find an Element,
> // we'll insert the next ordered Element in its place. Because the
> // content list is not live, it won't complain about an Element
> being
> // added twice.
> while(contentIter.hasNext())
> {
> Object obj = contentIter.next();
> if (obj instanceof Element)
> contentIter.set(childrenIter.next());
> }
>
> // Finally, we set the content list back into the parent Element.
> parent.setContent(null);
> parent.setContent(content);
> }
>
> But how can I sort elements by attribute?
> Please share the code example if you have.
>
> Thanks in advance.
>
> --
> View this message in context:
> http://www.nabble.com/How-can-I-sort-elements-by-an-attribute-value--tp15773258p15773258.html
> Sent from the JDOM - General mailing list archive at Nabble.com.
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/[email protected]
>



-- 
"Choć tyle wiemy własnym doświadczeniem:
W nas jest Raj, Piekło - i do obu - szlaki."
J.K.

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.