This post originated from an RSS feed registered with Ruby Buzz
by Adam Green.
Original Post: Data structures for sorting feed items
Feed Title: ruby.darwinianweb.com
Feed URL: http://www.nemesis-one.com/rss.xml
Feed Description: Adam Green's Ruby development site
My next step for feed aggregation is to combine all the feeds into a single RSS file with the items in reverse chronological order. As a database programmer my natural approach would be to write all of the feed items into a database and sort it in descending order on the date field. Unfortunately, I've decided to not use a database for the RubyRiver app, because I want to make this a tutorial that people can follow without having MySQL installed. I've already documented the hoops that I had to jump through to get MySQL installed in Ruby under Windows, and I don't want to put that in a tutorial. I also could use FeedTools, which looks great, but hides many of the details I want to expose. I realize that I'm imposing many artificial constraints, but as I keep saying, this is principally a teaching example, and I have strong opinions on how languages should be taught. If this was just about getting the job done, I'd be using anything that made life easier. I'll reserve database use and Feedtools for later tutorials.
Since I won't be using a database, I had planned on copying all the feed items into an array and then sorting it on the date column. Yes, I didn't know that Ruby doesn't have multi-dimensional arrays. That is exactly why I'm building some real apps in Ruby, because you can't understand a language by just reading about it. The things that aren't there don't always jump out at you. There are examples of Ruby code that will allow me to simulate a multi-dimensionay array and even sort it, but they are not the type of code I want to write, and certainly not the type of code for a tutorial.
What I am planning on doing to combine and sort the feed items is as follows:
Read the details of each feed item into a separate hash with a name based on a unique id for the item.
Create a single hash with the date of each item as the key and the id of its matching hash as the value.
Sort this master feedlist hash on the key.
Loop through the feedlist hash, and use it to point to the matching item hashes. Actually, after sorting it, the feedlist hash will be an array, but that will still work.
Write the data from each item hash out into a single RSS file.
As you can see, the relational model is in my blood.