# Help creating array/struct for select tag

**URL:** <https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797>\
**Category:** Rails\
**Created:** [July 22, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797 "2008-07-22T21:06:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/1 "2008-07-22T21:06:09Z")

</div>

here is my current code

\<%= select\_tag(:order, options\_for\_select(@orders, params[:order].to\_s))  
%\>

I am using some data that is not in a database so I generated a hash  
like the following:

@orders = {'Name =\> ‘name’, ‘Rank’ =\> ‘rank’, ‘HP’ =\> ‘hp’, ‘SP’ =\>  
‘sp’} etc…

it works fine but the problem I have is that the hash is never in the  
same order…after reading it looks like i should be using a struct or  
an array.

can someone help me convert this hash into an array or suggest the best  
way to do this?

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/2 "2008-07-22T22:12:29Z")

</div>

> it works fine but the problem I have is that the hash is never in the  
> same order…after reading it looks like i should be using a struct or  
> an array.
> 
> can someone help me convert this hash into an array or suggest the  
> best  
> way to do this?

@orders = [[‘Name’, ‘name’], [‘Rank’, ‘rank’], …]

Should do it. Or you might need to swap ‘name’ with ‘Name’. I can  
never remember 🙂

-philip

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/3 "2008-07-22T22:29:15Z")

</div>

> @orders = [[‘Name’, ‘name’], [‘Rank’, ‘rank’], …]
> 
> Should do it. Or you might need to swap ‘name’ with ‘Name’. I can  
> never remember 🙂
> 
> -philip

hmm…i don’t think it likes it.

it’s outputting this:

Namename Rankrank etc...

i tried doing this:

\<%= select\_tag(:order, options\_for\_select(@orders.collect{|order|  
[order[0], order[1]]}, params[:direction].to\_s)) %\>

but ended up with this output:

Namename Rankrank etc...

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/4 "2008-07-22T22:34:10Z")

</div>

Do you really need the difference in case? If not, try

@orders = %w(Name Rank SP HP)

I believe that will work…

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/5 "2008-07-22T22:48:23Z")

</div>

Roy P. wrote:

> Do you really need the difference in case? If not, try
> 
> @orders = %w(Name Rank SP HP)
> 
> I believe that will work…

Yea, actually that does work fine…i’m trying to order by that field in  
the database and i thought it was case sensitive in the SQL query.

however, i do have another sort field

@directions = {‘Ascending’ =\> ‘ASC’, ‘Descending’ =\> ‘DESC’}

and ASC and DESC might not be descriptive enough if you have any other  
ideas.

thanks for the help so far!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/6 "2008-07-22T22:41:48Z")

</div>

> > @orders = [[‘Name’, ‘name’], [‘Rank’, ‘rank’], …]
> > 
> > Should do it. Or you might need to swap ‘name’ with ‘Name’. I can  
> > never remember 🙂
> > 
> > -philip
> 
> hmm…i don’t think it likes it.

Strange. It should. Docs say:

options\_for\_select([[“Dollar”, “$”], [“Kroner”, “DKK”]])  
Dollar\nKroner\</  
option\>

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 23, 2008, 1:47am UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/7 "2008-07-23T01:47:12Z")

</div>

maybe i should just create two more tables : )

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [July 22, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/help-creating-array-struct-for-select-tag/143797/8 "2008-07-22T23:15:49Z")

</div>

Hi Scott!

> I am using some data that is not in a database so I generated a hash  
> like the following:
> 
> @orders = {'Name =\> ‘name’, ‘Rank’ =\> ‘rank’, ‘HP’ =\> ‘hp’, ‘SP’ =\>  
> ‘sp’} etc…
> 
> it works fine but the problem I have is that the hash is never in the  
> same order…after reading it looks like i should be using a struct or  
> an array.

AFAIK hashes are _not_ ordered _by definition_.

I know this may not be very helpful but it may explain _why_ you  
experience this behaviour.

Kind regards  
Martin
