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

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

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

Replies are listed 'Best First'.
Re^4: Sorting Template Toolkit Array of Hashrefs
by roho (Bishop) on Jul 19, 2012 at 10:35 UTC
    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" )

        Thanks for your reply. And thanks to my "really, really smart" co-worker who not only suggested sorting in Perl, but also provided an example of how to do it:

        my $sorted_results = [ sort { $a->{agendas}->[0]->{agendaDate} cmp $b- +>{agendas}->[0]->{agendaDate} } @$content ];

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