How to sort the result found

How to sort the result that I have found according to relevance. Let’s
say i have find all the users
@users=User.find(:all)
now in my view file I want to give a drop down box like sort
ascending,sort descending, sort by date which will cause the @users
sort accordingly.

use something like
@users=User.find(:all, :order => “created_at DESC”)
where :order is a SQL fragment, defining the sort order

Then in your view build your select tag (drop down menu) so that every
entry links to a function which calls index with a params value:

def index
params[:order] ||= “ccreated_at DESC”
@users = User.find(:all, :order => params[:order])
end

If you want to be uber picky about it you can have 2 drop down menus
one of which specifies the column to order by and another (drop down
or radio buttons) that say if its ASCending or DESCending and build
your query in a similar type of way.

def index
params[:order_column] ||= “created_at”
params[:direction] ||= “ASC”
@users = User.find(:all, :order => "#{params[:order_column]}
#{params[:direction]})
end

Thank you very much guys. But what I trying to do is that I don’t have
to go through the database yet another time when is choose sort by
date, sort asc or sort desc as the result is in @user object. Can not
I just manipulate this @user object only? Any suggestion will be
highly appreciated.
Thanks in advance

Thank you very much guys. But what I trying to do is that I don’t have
to go through the database yet another time when is choose sort by
date, sort asc or sort desc as the result is in @user object. Can not
I just manipulate this @user object only? Any suggestion will be
highly appreciated.

“When I choose” is the keypoint here.
Say you have done your query and a @user object.
Then it’s rendered and sent to the browser. The server immediately
forgets about it and the @user is gone.
If you select another search option in the browser and request the
list,
it will have to be rebuild anyway.
Even if this would not be the case: Sorting with Ruby code is
in nearly any case slower than a db request. Avoid it if you can.

Even if you would display two lists of the same object in one page,
it would be most likely much faster to do two db requests.

If it’s in the db, let the db handle the heavy work.

Thanks Thorsten M… I highly appreciate your suggestion and logics
you have pointed.
Thank you