# Sorting arrays - Any thoughts?

**URL:** <https://www.ruby-forum.com/t/sorting-arrays-any-thoughts/69542>\
**Category:** Rails\
**Created:** [September 21, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/sorting-arrays-any-thoughts/69542 "2006-09-21T18:17:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [September 21, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/sorting-arrays-any-thoughts/69542/1 "2006-09-21T18:17:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [September 21, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/sorting-arrays-any-thoughts/69542/2 "2006-09-21T18:48:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [September 21, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/sorting-arrays-any-thoughts/69542/3 "2006-09-21T20:07:01Z")

</div>

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

bookmarks.reverse.each do |b|

Thanks again…

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [September 25, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/sorting-arrays-any-thoughts/69542/4 "2006-09-25T16:54:46Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
