# Displaying array elements in order

**URL:** <https://www.ruby-forum.com/t/displaying-array-elements-in-order/143111>\
**Category:** Ruby\
**Created:** [July 15, 2008, 11:32am UTC](https://www.ruby-forum.com/t/displaying-array-elements-in-order/143111 "2008-07-15T11:32:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 15, 2008, 11:32am UTC](https://www.ruby-forum.com/t/displaying-array-elements-in-order/143111/1 "2008-07-15T11:32:14Z")

</div>

Hi,

for example

a=[12,354,633,68,85]  
\<%for i in a%\>  
\<%z=Document.find(i)%\>  
\<%=z.name%\>  
\<%end%\>

Here the result is displaying randomly in browser  
but i want to display the result in the array order  
i.e first 12 then followed by 354,633,68,85

If you have any solution plz reply

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 15, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/displaying-array-elements-in-order/143111/2 "2008-07-15T12:14:39Z")

</div>

On Jul 15, 2008, at 11:28, Pragash Mr. wrote:

> a=[12,354,633,68,85]  
> \<%for i in a%\>  
> \<%z=Document.find(i)%\>  
> \<%=z.name%\>  
> \<%end%\>
> 
> Here the result is displaying randomly in browser  
> but i want to display the result in the array order  
> i.e first 12 then followed by 354,633,68,85

If that’s happening, the array is ordered randomly before the `for`  
construct. Arrays are inherently ordered, so using a `for` will always  
yield the elements in the order they appear in the array. Try  
inspecting the array before looping over it to see which order the  
elements are in.

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 15, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/displaying-array-elements-in-order/143111/3 "2008-07-15T12:36:35Z")

</div>

Mikael HÃ¸ilund wrote:

> On Jul 15, 2008, at 11:28, Pragash Mr. wrote:
> 
> > a=[12,354,633,68,85]  
> > \<%for i in a%\>  
> > \<%z=Document.find(i)%\>  
> > \<%=z.name%\>  
> > \<%end%\>
> > 
> > Here the result is displaying randomly in browser  
> > but i want to display the result in the array order  
> > i.e first 12 then followed by 354,633,68,85
> 
> If that’s happening, the array is ordered randomly before the `for`  
> construct. Arrays are inherently ordered, so using a `for` will always  
> yield the elements in the order they appear in the array. Try  
> inspecting the array before looping over it to see which order the  
> elements are in.

Hi,  
Before the for loop also i checked but it is displaying in random order  
even though aray order is [12,354,633,68,85]  
but i have one doubt if it is ajax call then it will display in random  
order… plz reply
