# Sorting in ruby

**URL:** <https://www.ruby-forum.com/t/sorting-in-ruby/141769>\
**Category:** Rails\
**Created:** [June 30, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769 "2008-06-30T14:47:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [June 30, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/1 "2008-06-30T14:47:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [June 30, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/2 "2008-06-30T16:16:53Z")

</div>

On Jun 30, 7:23 am, Seb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 D. 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

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

[www.purpleworkshops.com](http://www.purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [June 30, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/3 "2008-06-30T16:42:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [July 7, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/4 "2008-07-07T14:00:28Z")

</div>

.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.

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [July 7, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/5 "2008-07-07T14:05:44Z")

</div>

@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)

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [July 8, 2008, 11:02am UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/6 "2008-07-08T11:02:44Z")

</div>

thanx…thorsten…now its working…

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [July 15, 2008, 9:02am UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/7 "2008-07-15T09:02:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![seb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8edcca/32.png) [@seb](https://www.ruby-forum.com/u/seb)\
**Post date:** [July 15, 2008, 9:32am UTC](https://www.ruby-forum.com/t/sorting-in-ruby/141769/8 "2008-07-15T09:32:25Z")

</div>

can you explain it a bit more…
