# Sorting the children of an object

**URL:** <https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029>\
**Category:** Rails\
**Created:** [December 13, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029 "2007-12-13T15:20:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029/1 "2007-12-13T15:20:39Z")

</div>

Hi  
another quick question.  
i have a relationship “areas has\_many elements” (elements acts\_as\_list)  
i want to retrive those elements ordered by element.position

elements = area.elements  
elements.each do |element|

is there a quick and elegent way to it (like area.elements.orderby)  
or is an SQL query a must.

like :  
Element.find(:all, condition =\> …, order =\> …

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029/2 "2007-12-13T15:53:16Z")

</div>

[http://www.ruby-doc.org/core/classes/Array.html#M002208](http://www.ruby-doc.org/core/classes/Array.html#M002208)  
Something like:  
elements.sort{ |x,y| x.position \<=\> y.position }

On Dec 13, 3:20 pm, Gady S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029/3 "2007-12-13T16:21:23Z")

</div>

You can actually do a find like such:

elements = area.elements.find(:all, :order =\> “name”)

and it will automatically scope only to elements in the area. Still a  
‘query’, but a very simple one.

Michael

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029/4 "2007-12-13T16:33:53Z")

</div>

thanks.  
these are good solutions.  
which one is better optimized ?  
Michael B. wrote:

> You can actually do a find like such:
> 
> elements = area.elements.find(:all, :order =\> “name”)
> 
> and it will automatically scope only to elements in the area. Still a  
> ‘query’, but a very simple one.
> 
> Michael

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029/5 "2007-12-13T17:45:08Z")

</div>

you can also sort through the association

has\_many :elements, :order =\> :position

so area.elements will always be sorted.

On Dec 13, 10:33 am, Gady S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 14, 2007, 4:31am UTC](https://www.ruby-forum.com/t/sorting-the-children-of-an-object/123029/6 "2007-12-14T04:31:11Z")

</div>

They are negligibly different in terms of efficiency. The  
has\_many :elements, :order =\> :name has a simplicity that I like, I  
would go with that one.

Michael B.  
removed\_email\_address@domain.invalid
