[Templates] Sorting a hash by value

Josh Rosenbaum josh@infogears.com
Thu, 31 May 2007 10:41:02 -0600


Jonathan Mangin wrote:
> Hi,
> 
> I have a simple hash with numeric id as key and
> alphabetic name as value.  In my template:
> 
> [% FOREACH key IN items.keys %]
>    [% IF key == rows.0.1 %]
>       <option value=[% key %] selected>[% items.$key %]
>    [% ELSE %]
>       <option value=[% key %]>[% items.$key %]
>    [% END %]
> [% END %]
> 
> %items is populating up to 20 drop-downs and I'd like to
> sort on value.  Is there a way to do that?
> 
> Thanks,
> Jon

http://www.template-toolkit.org/docs/plain/Manual/VMethods.html#Hash_Virtual_Methods

See the sort/nsort vmethods for hashes. I think that'll do what you want.

-- Josh