Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Sorting Template Toolkit Array of Hashrefs

by roho (Monsignor)
on Jul 18, 2012 at 15:47 UTC ( #982470=perlquestion: print w/ replies, xml ) Need Help??
roho has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to sort an array of hash refs in template toolkit. I'm using the sort syntax shown in the TT docs but the dates are not displayed in sorted order. What am I missing here? TIA

[% myobject = [ { 'agendas' => [ { 'agendaDate' => '2012-07-19T10:00:00.000- +04:00', 'id' => '361309' } ] }, { 'agendas' => [ { 'agendaDate' => '2012-07-25T10:00:00.000- +04:00', 'id' => '361410' } ] }, { 'agendas' => [ { 'agendaDate' => '2012-07-17T10:00:00.000- +04:00', 'id' => '361740' } ] } ] %] [% FOREACH row IN myobject.sort('agendas.0.agendaDate') %] [% 'date = ' _ row.agendas.0.agendaDate %]</br> [% END %]

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

Comment on Sorting Template Toolkit Array of Hashrefs
Download Code
Re: Sorting Template Toolkit Array of Hashrefs
by sundialsvc4 (Monsignor) on Jul 18, 2012 at 17:56 UTC

    Offhand, I question this:   sort('agendas.0.agendaDate')

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

        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

        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; %]
      Update: I just noticed the underlined .0 in your reply. This is necessary because 'agendas' is an array and I want the first entry in that array.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982470]
Approved by tinita
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-18 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (101 votes), past polls