# help creating array/struct for select tag

**URL:** <https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224>\
**Category:** rubyonrails-talk\
**Created:** [July 22, 2008, 7:06pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224 "2008-07-22T19:06:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 22, 2008, 7:06pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/1 "2008-07-22T19: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:** ![Martin\_Eisenhardt1](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Martin\_Eisenhardt1](https://discuss.rubyonrails.org/u/Martin_Eisenhardt1)\
**Post date:** [July 22, 2008, 7:10pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/2 "2008-07-22T19:10:13Z")

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

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [July 22, 2008, 8:11pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/3 "2008-07-22T20:11:34Z")

</div>

> \<%= 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?

@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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 22, 2008, 8:29pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/4 "2008-07-22T20: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:

\<select id="order" name="order"\> \<option value="Namename"\>Namename\</option\> \<option value="Rankrank"\>Rankrank\</option\> 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:

\<select id="order" name="order"\> \<option value=""\>Namename\</option\> \<option value=""\>Rankrank\</option\> etc...

---

<div class="post-metadata">

**Author:** ![Pardee\_Roy](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Pardee\_Roy](https://discuss.rubyonrails.org/u/Pardee_Roy)\
**Post date:** [July 22, 2008, 8:33pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/5 "2008-07-22T20:33:32Z")

</div>

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

&nbsp;&nbsp;@orders = %w(Name Rank SP HP)

I believe that will work...

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [July 22, 2008, 8:40pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/6 "2008-07-22T20:40:57Z")

</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"]]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<option value="$"\>Dollar\</option\>\n\<option value="DKK"\>Kroner\</

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 22, 2008, 8:48pm UTC](https://discuss.rubyonrails.org/t/help-creating-array-struct-for-select-tag/32224/7 "2008-07-22T20:48:23Z")

</div>

Roy Pardee wrote:

> Do you really need the difference in case? If not, try
> 
> &nbsp;&nbsp;@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!
