[dancer-users] Template Toolkit Sort Hash

Dave Cross dave at dave.org.uk
Sun Sep 27 13:38:49 BST 2015



On 27/09/15 12:48, Kadir Beyazlı wrote:
> On Sat, Sep 26, 2015 at 10:49 PM, Dave Cross <dave at dave.org.uk> wrote:
>>
>> On 26/09/15 19:25, Kadir Beyazlı wrote:
>>>
>>> Hi Dave,
>>>
>>> On Sat, Sep 26, 2015 at 12:11 PM, Dave Cross <dave at dave.org.uk
>>> <mailto:dave at dave.org.uk>> wrote:
>>>   >
>>>   >
>>>   > On 25/09/15 19:40, Richard Reina wrote:
>>>   >>
>>>   >> I display a hash reference in a  template.  It works well but is there a
>>>   >> way I can sort it by SNAME instead of ID?
>>>   >>
>>>   >>      <ul class="list-group fancy-list-items">    <!-- <ul
>>>   >> class="list-group checked-list-box"> -->
>>>   >>               <table style="width:100%">
>>>   >>                  <% FOREACH ID IN Pats.keys.sort %>
>>>   >>                  <tr class="list-group-item">
>>>   >>                    <td width="70"><% Pats.$ID.SNAME %></td>
>>>   >>                    <td width="75"><% Pats.$ID.ANAME %></td>
>>>   >>                    <td width="35"><% Pats.$ID.SSN %></td>
>>>   >>                    <td width="35"><% Pats.$ID.YR %></td>
>>>   >>                    <td width="250"><% Pats.$ID.CHNAME %></td>
>>>   >>                    <td width="550"><% Pats.$ID.DESCRIP %></td>
>>>   >>                    <% END %>
>>>   >>                  </tr>
>>>   >>              </ul>
>>>   >>              </table>
>>>   >
>>>   >
>>>   > It looks like you have a hash of hashes. Is that correct?
>>>   >
>>>   > Anyway, you can pass "sort" the name of the key that you want sort on.
>>> [KB] This is a very common problem asked previously and no solution
>>> found. It was advised to use array ref instead of hashref at previous
>>> mails. Now I tried your offer and see that it does not work. Values are
>>> unordered again.
>>
>>
>> Damn, yes. There were a couple of typos in my solution. I didn't have time to test it. Sorry about that.
>>
>> Of course, I can't be sure until I know what your data structure really looks like,
> [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
>
> $Pats = {ID => { SNAME => Value,
>                           CHNAME => Value,
>                           ANAME => Value,
>                       },
>              ID => { SNAME => Value,
>                           CHNAME => Value,
>                           ANAME => Value,
>                       },
>             ID => { SNAME => Value,
>                           CHNAME => Value,
>                           ANAME => Value,
>                       }
>              }
>
> ID is key of other attributtes. He asks if it id possible to sort by
> SNAME at above structure.
> I am using arrayref but this is common questin asked by beginners and
> no solution found

My second solution (shown below) works fine in this situation.

Dave...

>> but this demonstrates a solution for the data structure that seems to make most sense.
>>
>>    [% Pats = {
>>       id1 => {
>>         ID    => 'id1',
>>         SNAME => 'ZZZ',
>>         ANAME => 'aname1',
>>       },
>>       id2 => {
>>         ID    => 'id2',
>>         SNAME => 'XXX',
>>         ANAME => 'aname2',
>>       },
>>       id3 => {
>>         ID    => 'id3',
>>         SNAME => 'YYY',
>>         ANAME => 'aname3',
>>       }
>>    } -%]
>>    Original:
>>    [% FOREACH id IN Pats.keys.sort -%]
>>    [% id %] / [% Pats.$id.SNAME %] / [% Pats.$id.ANAME %]
>>    [% END -%]
>>    New:
>>    [% FOREACH Pat IN Pats.values.sort('SNAME') -%]
>>    [% Pat.ID %] / [% Pat.SNAME %] / [% Pat.ANAME %]
>>    [% END -%]
>>
>> Cheers,
>>
>> Dave...
>>
>>
>> _______________________________________________
>> dancer-users mailing list
>> dancer-users at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
>
>

-- 
Dave Cross :: dave at dave.org.uk
http://dave.org.uk/
@davorg


More information about the dancer-users mailing list