[Templates] Sorting a hash by value
Jonathan Mangin
jon.mangin@comcast.net
Thu, 31 May 2007 14:03:40 -0400
Josh,
I wondered why items.sort wasn't working, until I spotted
another syntax error. Thanks for making me look again.
--Jon
----- Original Message -----
From: "Josh Rosenbaum" <josh@infogears.com>
To: "Jonathan Mangin" <jon.mangin@comcast.net>
Cc: <templates@template-toolkit.org>
Sent: Thursday, May 31, 2007 12:41 PM
Subject: Re: [Templates] Sorting a hash by value
> 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