Quick question to see how I might re-sort an Activerecord data set. Let''s say I have this table: tablename: testscores | id | student | score | avg_for_year 1, steve, 85, 82 2, tom, 84, 92, 3, jim, 92, 95 In my controller, I have something like: t= Testscores.find(:all, :order => "score DESC", :limit => 2) ...where it would return the records for Jim and Steve.... My question is: Is it possible to sort the resulting array "t" by name? Where if I want to render this collection in a view, I could render it sorted by the students name, rather than their test score. The key point is that I am taking only 2 of the 3 records based on their test score and then sorting the returned students alphabetically rather than by test score. I''ve tried things like t.sort.name and other variations. I also thought that I could loop through t and push data to a new array and sort that, but it doesn''t seem very clean. My example is more complicated but that is the gist. Thanks. Steve Odom http://www.smarkets.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Dec 26, 2005, at 11:27 AM, Steve Odom wrote:> Quick question to see how I might re-sort an Activerecord data set. > Let''s say I have this table: > > tablename: testscores > | id | student | score | avg_for_year > 1, steve, 85, 82 > 2, tom, 84, 92, > 3, jim, 92, 95 > > In my controller, I have something like: > t= Testscores.find(:all, :order => "score DESC", :limit => 2) > > ...where it would return the records for Jim and Steve.... > > My question is: > Is it possible to sort the resulting array "t" by name?t = t.sort_by{|test| test.student } That will sort the records in alphabetical order by the student name. Then you should be able to iterate through them like normal except they will be in the order you want. -Ezra> Where if I want to render this collection in a view, I could render > it sorted by the students name, rather than their test score. > > The key point is that I am taking only 2 of the 3 records based on > their test score and then sorting the returned students > alphabetically rather than by test score. > > I''ve tried things like t.sort.name and other variations. I also > thought that I could loop through t and push data to a new array > and sort that, but it doesn''t seem very clean. > > My example is more complicated but that is the gist. > > Thanks. > Steve Odom > http://www.smarkets.net > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Damn Ezra, I''m going to have to make my email signature say "Thanks Ezra" because it would save me a lot of typing. Thanks Ezra. Steve On 12/26/05, Ezra Zygmuntowicz <ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org> wrote:> > > On Dec 26, 2005, at 11:27 AM, Steve Odom wrote: > > Quick question to see how I might re-sort an Activerecord data set. Let''s > say I have this table: > > tablename: testscores > | id | student | score | avg_for_year > 1, steve, 85, 82 > 2, tom, 84, 92, > 3, jim, 92, 95 > > In my controller, I have something like: > t= Testscores.find(:all, :order => "score DESC", :limit => 2) > > ...where it would return the records for Jim and Steve.... > > My question is: > Is it possible to sort the resulting array "t" by name? > > > t = t.sort_by{|test| test.student } > > > That will sort the records in alphabetical order by the student name. Then > you should be able to iterate through them like normal except they will be > in the order you want. > > -Ezra > > > Where if I want to render this collection in a view, I could render it > sorted by the students name, rather than their test score. > > The key point is that I am taking only 2 of the 3 records based on their > test score and then sorting the returned students alphabetically rather than > by test score. > > I''ve tried things like t.sort.name and other variations. I also thought > that I could loop through t and push data to a new array and sort that, but > it doesn''t seem very clean. > > My example is more complicated but that is the gist. > > Thanks. > Steve Odom > http://www.smarkets.net > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > 509-577-7732 > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails