Sorting arrays - Any thoughts?

I’m using the “rubilicious” gem to integrate delicious into my site, and
it’s working fine. However, the items are out of order. It pulls the
most recent 10 posts, but not in descending order. This is what I’m
doing:

def setup_rubilicious
require ‘rubilicious’
r = Rubilicious.new(‘user’,‘pass’)
@d_links = Array.new
@d_descs = Array.new
@d_times = Array.new
@per_page = 10
bookmarks = r.recent(‘rpheath’,@per_page)
bookmarks.each do |post|
@d_links << post[‘href’]
@d_descs << post[‘description’]
@d_times << post[‘time’]
end
end

So, essentially I need to sort @d_links, @d_descs, @d_times according to
the date in descending order, so they all match up. Is there a better
way to do this than use 3 variables? It would be ideal to have one
variable, and refer to the “time” or “description” or “href”. Something
like:

@delicious.each do |post|
<a href="<%= post[“href”] %>" title="<%= post[“time”] %>"><%=
post[“description”] %>

end

But if that’s not possible, what is the best way to sort all three
variables based on the time??? Thanks!

I didn’t understand where the date was (assuming it’s post[‘time’]), but
sort_by is a cool thing:

bookmarks = r.recent(‘rpheath’,@per_page).sort_by { |post| post[‘time’]
}

Vish

Thanks! That worked perfectly. I actually needed the reverse list, but
that’s also easy using:

bookmarks.reverse.each do |b|

Thanks again…

On Sep 21, 2006, at 2:07 PM, ry an wrote:

Thanks! That worked perfectly. I actually needed the reverse
list, but
that’s also easy using:

bookmarks.reverse.each do |b|

Thanks again…

If the time is a number, then negate it for the sort_by:

bookmarks = r.recent(‘rpheath’,@per_page).sort_by { |post| - post
[‘time’] }

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs