[Catalyst] Sort something in the stash
A. Pagaltzis
pagaltzis at gmx.de
Thu Jun 8 03:37:37 CEST 2006
* Ryan <god at detz.net> [2006-06-07 15:10]:
> This probably already exists in perl but it's something I've
> come upon a few times and I was seeing if there was a way to do
> it.
>
> I use Template to view the pages so for data I store it in a
> array of hashes so I can loop and grab the data.
>
> $c->stash->{results}->[0]->{id} = 1;
> $c->stash->{results}->[0]->{name} = 'test';
>
> Something like that...and I want to be able to sort on say
> 'name' in that structure. Anyone know an easy way to sort by an
> array of hashes on a key?
perldoc Template::Manual::VMethods under “List Virtual Methods”:
sort, nsort
Returns the items in alpha (sort) or numerical (nsort) order.
[% library = books.sort %]
An argument can be provided to specify a search key. Where an
item in the list is a hash reference, the search key will be
used to retrieve a value from the hash which will then be
used as the comparison value. Where an item is an object
which implements a method of that name, the method will be
called to return a comparison value.
[% library = books.sort(’author’) %]
In the example, the ’books’ list can contains hash references
with an ’author’ key or objects with an ’author’ method.
Regards,
--
#Aristotle
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1};
&Just->another->Perl->hacker;
More information about the Catalyst
mailing list