# Order for a each loop

**URL:** <https://www.ruby-forum.com/t/order-for-a-each-loop/171277>\
**Category:** Rails\
**Created:** [June 29, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/order-for-a-each-loop/171277 "2009-06-29T23:31:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 29, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/order-for-a-each-loop/171277/1 "2009-06-29T23:31:13Z")

</div>

I have a model for users and another model for linenumbers and I have  
some code that looks like:

@user.linenumbers.each do |line|

end

and it loops through a user and their line numbers. What if I want to  
order this list, is there a way to do that?

I know I could just put these linenumbers into an a find statement in my  
controller and order it that way, but I would rather not do that way.  
Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 30, 2009, 12:37am UTC](https://www.ruby-forum.com/t/order-for-a-each-loop/171277/2 "2009-06-30T00:37:45Z")

</div>

On Jun 29, 5:31 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a model for users and another model for linenumbers and I have  
> some code that looks like:
> 
> @user.linenumbers.each do |line|
> 
> end
> 
> and it loops through a user and their line numbers. What if I want to  
> order this list, is there a way to do that?

Well you can give an :order option to the association yourself.  
Alternatively you could call sort or sort\_by on your array

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 30, 2009, 12:44am UTC](https://www.ruby-forum.com/t/order-for-a-each-loop/171277/3 "2009-06-30T00:44:11Z")

</div>

On Jun 29, 2009, at 5:31 PM, Shandy N. wrote:

> I know I could just put these linenumbers into an a find statement  
> in my  
> controller and order it that way, but I would rather not do that way.  
> Thanks,
> 
> ## -S

Well, after first thinking “Why not?” I’ll offer this (it’s only Ruby):

@user.linenumbers.sort\_by{|line| line.number}.each do |line|  
#…  
end

But I’ll still suggest:

class User  
has\_many :linenumbers, :order =\> ‘number’  
end

Or perhaps a named scope is OK,

class Linenumber  
named\_scope :ordered, :order =\> ‘number’  
end

@user.linenumbers.ordered.each do |line|  
#…  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 30, 2009, 10:20am UTC](https://www.ruby-forum.com/t/order-for-a-each-loop/171277/4 "2009-06-30T10:20:19Z")

</div>

2009/6/29 Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > order this list, is there a way to do that?
> 
> Or perhaps a named scope is OK,
> 
> class Linenumber  
> Â named\_scope :ordered, :order =\> ‘number’  
> end

Or  
default\_scope :order =\> ‘number’  
if linenumbers are always to be sorted.

Coli9n

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 30, 2009, 4:35pm UTC](https://www.ruby-forum.com/t/order-for-a-each-loop/171277/5 "2009-06-30T16:35:32Z")

</div>

Perfect, exactly what I wanted, thank you both.

-S
