title: Sorting an XML in Java
body: <p>I have an XML similar to below, which needs to be sorted using the date field.</p>
  
  <pre><code>&lt;root&gt; 
      &lt;Node1&gt;
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node1&gt; 
      &lt;Node1&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node1&gt; 
      &lt;Node1&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node1&gt; 
      &lt;Node1&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node1&gt; 
      &lt;Node2&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node2&gt; 
      &lt;Node2&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node2&gt; 
      &lt;Node2&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node2&gt; 
      &lt;Node2&gt; 
          &lt;date&gt;&lt;/date&gt; 
      &lt;/Node2&gt; 
  &lt;/root&gt;
  </code></pre>
  
  <p>I would like to sort the XML based on the date(say ascending order), irrespective of whether the date is under Node1 or Node2. Actually, in Java code I have two separate lists, one with Node1 objects and other with Node2 objects. I can sort the list in any order separately inside java. But I need to have the dates sorted irrespective of the nodes it is appearing on the XML. <strong>What is the best approach to sort this way in Java?</strong> </p>
  
  <p>Actually I am using Castor for marshaling the java objects to XML. If you know this can be done with Castor, that will be great! </p>