[Templates] Sorting numerically?
Andy Wardley
abw@cre.canon.co.uk
Thu, 10 Aug 2000 20:49:08 +0100 (BST)
> I've searched through the docs and Dejanews but have not found anything for
> numeric sorting
>
> [% FOREACH order_number = order_record.keys.sort { $a <=> $b } %]
>
> I also looked at the source but I wasnt clear on where the sorting is
> actually taking place.
The quick hack would be to look in Template/Context.pm around line
596 for $list_ops and add your own sort function to the hash array.
e.g.
'nsort' => sub { my $list = shift; [ sort { $a <=> $b } @$list ] },
[% FOREACH order_number = order_record.keys.nsort %]
In version 2, it's $Template::Stash::LIST_OPS which you can set from
a script without needing to hack the source code.
$Template::Stash::LIST_OPS->{ nsort } => sub { ... };
A numerical sort is probably worth adding as a permanent feature,
so I'll probably squeeze it into version 2. But there should probably
be some better way to do this for the general case.
Hope this helps.
A