Changing output from my hash

i have a program who gets urls and category from pages on a certain
directory, i put this data on a hash like this example:

pages{general =>[link1,link2)],producst => [link1, link2], services =>
[link1, link2], downloads => [link1,link2]}

I’m using haml on my view page
so the output code looks like this

  • pages.each do |category, links|
    .span-6
    %h1= category
    %ul
    - links.each do |link|
    %li= link

so this give me the result:
Downloads General products services
link1 link1 link1 link1
link2 link2 link2 link2

i wish the general come to the left side and the downloads go to right,
is there any way to sort my hash?

On 12/29/2011 07:28 AM, alvaro alves wrote:

.span-6

i wish the general come to the left side and the downloads go to right,
is there any way to sort my hash?

Sort your hash keys and then iterate over the sorted list in order to
pull the values from the hash in the same order. I’m not familiar with
HAML, but here is my guess as to what it should be using a hard coded
ordered list of categories:

  • ordered_categories = %w(general products services downloads)
  • ordered_categories.each do |category|
  • links = pages[category]
    .span-6
    %h1= category
    %ul
    - links.each do |link|
    %li= link
    end

If your categories are more dynamic, you’ll need to find a more creative
way to sort them. The keys method on the pages variable will give you
the keys of the hash as an array, and you can use the sort method of
that to go wild. :slight_smile:

-Jeremy

Thanks a lot this works to my app :wink:

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