Sorting in ruby

I am new in ruby.I want to know how sorting can be done in ruby based
on some fields like customer name,location etc.what i meant is i have
to sort the table based on some fields.how can i.?Can anybody help me.

I'll assume you're asking about sorting models in Rails? Of course
you can, and I'll give you an example.

(But good etiquette on this list (and any other) is to give more
details about what you're doing, and what you've tried so far, so that
it's evident you've tried something first. Please think about that
for next time.)

In any event, here's how you typically select all your Customers
sorted by name (I'm assuming you have a model named 'Customer' with an
attribute called 'name'):

Customer.find :all, :order => :name # orders by name

Also, if you haven't already, I'd suggest you start with a good Rails
primer like Agile Web Development with Rails, or look for the highest-
rated Rails books on Amazon. Once you spend a little time with the
basics, I think you'll really enjoy using Rails.

Jeff
www.softiesonrails.com
www.purpleworkshops.com

Note Jeff's way is better, sometimes you will need to use this way as
well:

@collection = Model.find(:all) (or whatever)
@collection2 = Model.find(:all) or whatever
@collection = [@collection, @collection2].flatten

@collection.uniq! #which removes duplicates using the @collection
array

@collection.sort! {|a, b| a.name <=> b.name}

This is helpful when you are making a larger collection out of two
smaller ones and couldn't get the correct .find statement. But go
with Jeff's way if at all possible.

.now i have another problem .i want to sort my table based on customer
name in my project listing.my customer name is in customer table not
in my project table.Im my project table i have customer_id .now i'm
only able to sort it based on customer_id.but i want to sort based on
customer name.
in my index file i hav
<%= link_to "Customer" ,
    :action => 'sort',
    :identity => '3'
in my controller i hav
def sort
    id = params[:identity]
     if id == '1'
      @projects = Project.find :all, :order => :customer_id
its working fine.but i want to sort it based on custmer name.can
anybody help me.

@projects = Project.find :all, :order => "customers.name", :include
=> :customer

the include will make this a "eager loading", where projects &
customers are joined
Note the "customers.name" which is the table name and column name
as in the database (so plural, not singular for customers)

thanx....thorsten..now its working.......

now i have another problem..i want too use some conditions with
sorting..tried to add. but not working...
in my index file i hav
<%= link_to "Customer" ,
    :action => 'sort',
    :identity => '3'%>
in my controller i hav
def sort
    id = params[:identity]
     if id == '1'
@projects = Project.find (:all, :order => "customers.name", :include
=> :customer)
i want to add this query..
@projects = Project.find_by_sql ["SELECT * FROM projects p,
memberships m WHERE p.id = m.project_id AND m.user_id =?", mem_id]
along with the previuos sorting..That's i want to display the fields
which should satisfy this condition along with sorting ..how can i?

can you explain it a bit more...