Forum: Ruby on Rails how to do sorting in ROR

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
46911700017303070d64f73b69bb04a8?d=identicon&s=25 Guest (Guest)
on 2007-02-21 12:56
hi,

  is there any command for sorting the list box items in ROR. if its
please tell me
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-02-21 13:35
(Received via mailing list)
Hi, are the list box items being retrieved from the DB?  If so, you
can simply use the ':order' within the find method of your model.  For
example,

orders = Order.find(:all, :order => ["pay_type, shipped_at DESC"],
:conditions => "id = ?", 25 )

If not, please provide more information as to how you're populating
the list box.

Good luck,

-Conrad
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-02-21 21:58
Conrad Taylor wrote:
> Hi, are the list box items being retrieved from the DB?  If so, you
> can simply use the ':order' within the find method of your model.  For
> example,
>
> orders = Order.find(:all, :order => ["pay_type, shipped_at DESC"],
> :conditions => "id = ?", 25 )
>
> If not, please provide more information as to how you're populating
> the list box.
>
> Good luck,
>
> -Conrad

You can also sort any array of object by some method or attribute.

  orders.sort {|a, b| a.total <=> b.total}
This topic is locked and can not be replied to.