Resorting a dataset

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 O.
http://www.smarkets.net

On Dec 26, 2005, at 11:27 AM, Steve O. wrote:

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

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

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