http://www.perlmonks.org?node_id=982513


in reply to Re: Sorting Template Toolkit Array of Hashrefs
in thread Sorting Template Toolkit Array of Hashrefs

The TT documentation says ...

"When 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".

Since row.agendas.0.agendaDate retrieves the value inside the FOREACH loop, it seems reasonable that it would retrieve the value for the sort comparison, but alas it's not doing that. :(

"Its not how hard you work, its how much you get done."

  • Comment on Re^2: Sorting Template Toolkit Array of Hashrefs

Replies are listed 'Best First'.
Re^3: Sorting Template Toolkit Array of Hashrefs
by Anonymous Monk on Jul 19, 2012 at 01:22 UTC

    Since row.agendas.0.agendaDate retrieves the value inside the FOREACH loop,

    But that is not a (singular) key or method , it is a compound expression

    Template::Manual::VMethods says

    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.

    To see how its implemented view sub Template::VMethods::list_sort

      So that begs the question ... how to turn the compound expression into a singular key?

      "Its not how hard you work, its how much you get done."

        So that begs the question ... how to turn the compound expression into a singular key?

        Don't :)

        If you view the source, you can see the current implementation

        If you're smart enough you could alter it to accept  [ agendas', 0, 'agendaDate' ] to sort by agendas.0.agendaDate

        If you're really smart, you'll do any such sorting/prep required from within perl and simplify the data structure you give the template ( adjust your "model" )

Re^3: Sorting Template Toolkit Array of Hashrefs
by Anonymous Monk on Mar 07, 2013 at 10:37 UTC

    This is how I explored before source diving in Sorting Template Toolkit Array of Hashrefs

    [%# ## tpage sort.compound.tt ## tpage --debug=2047 sort.compound.tt ## tpage --debug=all sort.compound.tt ## ## tpage --debug=off sort.compound.tt ## tpage --debug=vars sort.compound.tt ## tpage --debug=filters sort.compound.tt ## tpage --debug=dirs sort.compound.tt ## tpage --debug=provider sort.compound.tt ## tpage --debug=plugins sort.compound.tt #~ unknown debug flag ## tpage --debug=flags sort.compound.tt ## tpage --debug=stash sort.compound.tt ## tpage --debug=undef sort.compound.tt ## tpage --debug=context sort.compound.tt ## tpage --debug=caller sort.compound.tt ## tpage --debug=parser sort.compound.tt ## tpage --debug=all sort.compound.tt ## tpage --debug=on sort.compound.tt ## tpage --debug=service sort.compound.tt ## ## http://perlmonks.org/?node_id=982470# Re^3: Sorting Template Toolki +t Array of Hashrefs ## ## ## ## COMMENTS END -%] [% SET results = [ { 'a' => [ { 'b' => 6 } ] }, { 'a' => [ { 'b' => 3 } ] }, { 'a' => [ { 'b' => 4 } ] }, { 'a' => [ { 'b' => 9 } ] }, ]; %] [% USE Dumper Indent = 0; GET Dumper.dump( results ); %] ## [% FOREACH item IN results.sort %] [% USE Dumper Indent = 0; GET Dumper.dump( item ); -%] [% END %] ## [% FOREACH item IN results.nsort('a.0.b') %] [% USE Dumper Indent = 0; GET Dumper.dump( item ); -%] [% END %] #################### #################### [% SET results = [ { 'a' => [ { 'b' => 6 } ], 'a.0.b' => 6 }, { 'a' => [ { 'b' => 3 } ], 'a.0.b' => 3 }, { 'a' => [ { 'b' => 4 } ], 'a.0.b' => 4 }, { 'a' => [ { 'b' => 9 } ], 'a.0.b' => 9 }, ]; USE Dumper Indent = 0; FOREACH item IN results.nsort('a.0.b'); GET Dumper.dump( item ); GET "\n"; END; %]