Created
September 4, 2013 09:16
-
-
Save kmelkon/6434652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@grouped = {} | |
Deal.all.each do |deal| | |
letter = deal.background.slice(0,1).upcase | |
@grouped[letter] ||= [] | |
@grouped[letter] << deal | |
end |
<% @grouped.keys.sort.each do |letter| -%>
<% @grouped[letter].each do |deal| -%>
<%= deal.background %>
<% end %>
<% end %>
<% @grouped.sort.each do |letter, deal| -%>
<%= deal.background %>
<% end %>
Calling #sort on a Hash will turn it into a nested array:
{"b" => "x", "a" => "y"}.sort
# => [["a", "y"], ["b", "x"]]
Fortunately #each is smart enough to still support .each do |first_value_in_nested_array, second_value_in_nested_array|
on that array, so the loop will just work like it's a Hash.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might need a
.to_a
afterDeal.all