Sorting an XML in Java
        Posted  
        
            by Java Guy
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Java Guy
        
        
        
        Published on 2010-05-26T22:00:44Z
        Indexed on 
            2010/05/26
            22:11 UTC
        
        
        Read the original article
        Hit count: 436
        
Hello I have an XML similiar to below, which needed to be sorted using the date field.
<root> 
    <Node1><date></date></Node1> 
    <Node1><date></date></Node1> 
    <Node1> <date></date></Node1> 
    <Node1> <date></date></Node1> 
    <Node2> <date></date></Node2> 
    <Node2> <date></date></Node2> 
    <Node2> <date></date></Node2>
   <Node2> <date></date> </Node2> 
   </root>
I would like to sort the XML based on the date(say asc order), irrespective of whether the date is under Node1 or Node2. Actually in Java code I have two seperate lists, one with Node1 objects and other with Node2 obects. I can sort the list in any order sperately inside java. But I need to have the dates sorted irrespective of the nodes it is apperaing on the XML. What is the best approach to sort this way in Java?
Actaully I am using Castor for marshalling the java objects to XML. If you know this can be done with Castor, that will be great!
© Stack Overflow or respective owner