开发者

Alphabetical lists with ruby on rails

I am building a list that will be sorted by the alphabet a开发者_如何学JAVAnd am looking for a solution to grab the database result and sort it alphabetically.

Any help is greatly appreciated!


Building on thenduks above, I like:

Company.rb

def initial
  return '?' if name.blank?
  # name.[0].upcase (updated to get the first character )
  name.slice(1).chr.upcase
end

view

<% # Company.all.group_by(&initial) do |initial, companies| (updated) %>

<% Company.all.group_by(&:initial).each do |initial, companies| %>
  <%= content_tag(:h2, initial)%>
  <% companies.each do |company|%>
    <%= link_to(company.name, company%>
  <% end %>
<% end %>


Hmm, if you have a list that isn't too massive you might just do it naively like:

(This assumes you have a model Company which has a name attribute)

@grouped = {}
Company.all.each do |company|
  letter = company.name.slice(0,1).upcase
  @grouped[letter] ||= []
  @grouped[letter] << company
end

And now you can, in your view, do something such as:

<ul>
  <% @grouped.keys.sort.each do |letter| -%>
    <li>
      <h2><%= letter %></h2>
      <ul>
        <% @grouped[letter].each do |company| -%>
          <li><%= company.name %></li>
        <% end -%>
      </ul>
    </li>
  <% end -%>
</ul>

Update: If you want to extend the logic on what the 'letter' is, you would probably move the logic into the model, eg:

class Company
  # ... code

  def initial
    # find a number at the start of the string if it exists
    m = self.name.match(/^\d+/)
    return m[0] if m
    # or return the first letter upcased otherwise
    return self.name.slice( 0, 1 ).upcase
  end
end


Try this:

g = Company.all.group_by{|c|c.name.upcase[0..0]}
('A'..'Z').each do |letter|
  companies = g[letter] || []
  # process the name and the letter
end

This will work even in scenarios when you do not have items for few letters.

Note: You might want to handle the numbers and non English letters differently by modifying the group_by logic. You also have to change the letter name iteration list accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜