I have a perl multi dimensional hash reference that I iterate in Template Toolkit in the following manner. Is there a way for it to be sorted by one of the values like AVG instead of by the key?
<tr>
<% FOREACH name in payload_batting.keys.sort %>
<td width="140"><% name.substr(0, 18);%></td>
<td width="55"><% payload_batting.$name.atts %></td>
<td width="55"><% payload_batting.$name.runs %></td>
<td width="55"><% payload_batting.$name.hits %></td>
<td width="55"><% payload_batting.$name.AVG %></td>
<tr>
<% END %>
As ikegami said, you have to sort it for yourself. If you wanna do the sort in the presentation layer, in Template Toolkit, you can do the sort in the temlpate itself as a Perl code. For this you have to set EVAL_PERL to true value. Then the code would look something like this
What would be much cleaner is to register a new custom virtual method that would do the sort for you (documentation is here):
First you have to define the virtual method that implements the key sort for you. This is only a simple implementation allowing you to sort nested hashes by multiple levels down in the structure:
Then you have to refer to it in the template like so:
Different case with deeper data structure: