'Sort RSS items by date in Ruby

I am combining multiple items into one RSS file iterating them like so:

# create RSS
atom = RSS::Maker.make('atom') do |f|
  f.channel.updated = Date.today.to_s

  # iterate through Ideas
  ideas[0,50].each do |r|
    f.items.new_item do |e|
      e.updated = DateTime.parse(r[:date].to_s).to_s
      e.title = r[:title]
      e.content.type = 'html'
    end
  end

  # iterate through Articles
  articles[0,50].each do |r|
    f.items.new_item do |e|
      e.updated = DateTime.parse(r[:date].to_s).to_s
      e.title = r[:title]
      e.content.type = 'html'
    end
  end
end

But the order of the items in the file is not sorted by date (because I loop through the first batch “Ideas” then ”Articles”)

I tried after iterating to sort by date / updated value, with the following combos:

  # f.items.sort {|a,b| a.updated <=> b.updated}
  # atom.items.sort! {|a,b| a.date <=> b.date}
  # f.items.reverse!
  # f.items.sort_by(&:date)
  # f.sort_by(&:date)

But without success.

Do I need to loop again through each item in the RSS to sort them?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source