title: How to sort_by dates in csv using ruby
body: <p><a href="https://i.stack.imgur.com/u5aAG.png" rel="nofollow noreferrer">This my csv output</a>,I need to sort the rows according to the earliest date of States node.</p>
  
  <pre><code>require 'nokogiri'
  require 'csv'
  xmlfile = File.read("test3.xml")
  doc = Nokogiri::XML(xmlfile)
  all = []
  doc.css('Firm').each_with_index do |firm,i|
   firstchild  = []
   secondchild =[]
  firm.css('States').each do |rgltr_addr_node|
   if rgltr_addr_node.has_attribute?("RgltrCd")
    RgltrCd =rgltr_addr_node.attributes["RgltrCd"]&amp;&amp;rgltr_addr_node.attributes["RgltrCd"].value
   else 
   RgltrCd = "NA"
  end
  if rgltr_addr_node.has_attribute?("St")
    St = rgltr_addr_node.attributes["St"] &amp;&amp; rgltr_addr_node.attributes["St"].value
  else
  St = "NA"
  end
  if rgltr_addr_node.has_attribute?("Dt")
    Dt = rgltr_addr_node.attributes["Dt"] &amp;&amp; rgltr_addr_node.attributes["Dt"].value
  else 
  Dt ="NA"
  end
  firstchild[0] = RgltrCd 
  firstchild[1] = St
  firstchild[2] = Dt
  end
  firm.css('Filing').each do |filing_node|
  if filing_node.has_attribute?("Dt")
    Dt = filing_node.attributes["Dt"] &amp;&amp; filing_node.attributes["Dt"].value
  else
  Dt ="NA"
  end
  if filing_node.has_attribute?("FormVrsn")
    FormVrsn = filing_node.attributes["FormVrsn"] &amp;&amp; filing_node.attributes["FormVrsn"].value
  else 
  FormVrsn ="NA"
  end
    secondchild[0] = Dt 
    secondchild[1] = FormVrsn
   end
  end
  all &lt;&lt; firstchild + secondchild 
  end
  </code></pre>
  
  <p>I used the following looping for data to display in each row of csv:</p>
  
  <pre><code>CSV.open('new_test3_file.csv', 'wb' ) do |row|
      row &lt;&lt; ['States', 'Filing']
      all.each do |data|
          row &lt;&lt; data
      end
  </code></pre>
  
  <p>Can any one help me in sorting this by date?</p>