Okay, here’s my question. I have data in a MySQL table and I want to
display the data in the alphabetical order of each row’s title. Right
now it displays it in the order it’s added. Is there a simple method I
can add to make it switch to alphabetical order, similar to how the
.reverse method? I’m really new, so this might not make sense…
Here’s the code as I have it now in my list.rhtml:
Okay, here’s my question. I have data in a MySQL table and I want to
display the data in the alphabetical order of each row’s title. Right
now it displays it in the order it’s added. Is there a simple method I
can add to make it switch to alphabetical order, similar to how the
.reverse method? I’m really new, so this might not make sense…
In your controller, change the line that looks like this:
<% for recipe in @recipes.sort{|a,b| a.title <=> b.title } %>
Jon G. … you are awesome… I struggled the entire day yesterday
figuring out how to sort a dynamic list based on categories
alphabetically while working on creating a styleguide based on ruby. I
have no experience working on ruby. When i found this link via google i
finally got my Eureka Moment. Thanks once again for providing the code.
The magic code that helped me was .sort{|a,b| a<==>b}
just to provide the entire code in context i used is a below