Brett Sanger <[EMAIL PROTECTED]> writes:
> So I have an array of hashrefs, that have date-related data (frex:
> Month and year)
> I've been asked to sort the output in most-recent-first order.
>
> I tried: [% FOREACH Report = Reports.nsort('year').nsort('month') %]
> (I expected this to be backwards, but I figured I'd mess with
> reverse once I was sure this worked)
Reports.nsort('year') yields a list, sorted according to the years.
Then you sort *this whole list* again, this time according to the
month. This second nsort doesn't know nor care about values under the
'year' key.
I'd be surprised if your results are different from evaluating
[% FOREACH Report = Reports.nsort('month') %]
For similar problems I've always been creating intermediate keys:
[% FOREACH Report = Reports %]
[% Report.sortby = Report.month + ( 12 * Report.year ) %]
[% END %]
[% FOREACH Report = Reports.nsort('sortby').reverse %]
This isn't extremely efficient, but works from plain TT.
> This didn't quite work as expected. I got the results I wanted
> with:
>
> [% FOREACH Report = Reports.nsort('month').nsort('year').reverse %]
>
> But I'm not sure that that's not just an artifact of my data.
Well, it's "better" in a sense that it sorts according to the years.
But nevertheless it's an artifact.
> The docs (Template::Manual::VMethods) don't seem to talk about
> "stacking" vmethods...what is the expected order of resolution? ...
It's left to right, and it is so straightforward that I sometimes find
myself writing enormous vmethod chains....
The range of years covered by this reports is [%
Reports.nsort('year').first.year %] - [%
Reports.nsort('year').last.year %]
(of which I'm not very proud afterwards :-))
> ... Does the sort vmethod respect the previous order where there is
> no change required?
No.
--
Cheers, haj
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates