holli has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

I just ran into an issue with the Template-Toolkit and I'm not sure wether I miss something obvious or if it is a bug in the Toolkit.

On my webserver I have a directory that looks like this:
C:\xampp\htdocs\messages 20050121.html 20050122.html 20050123.html 20050124.html 20050125.html 20050126.html 20050127.html 20050128.html 20050129.html 20050130.html 20050913.html 20051124.html
The names are in the YYYYMMDD-format. In a template I iterate over this directory using
<table cellpadding="0" cellspacing="0"> <tr> <td colspan="2"> [% USE dumper(indent=0, pad="<br>") %] [% USE dir = Directory('/xampp/htdocs/messages') %] [% FOREACH file = dir.files.sort %] <p>[% file.name %]</p> <p>[%# file.contents %]</p> [% END %] </td> </tr> </table>
I also tried
[% FOREACH file = dir.files.sort(file.name) %] [% FOREACH file = dir.files.sort(name) %]
But none of them seems to work, because in the output the files are in this order:
20050128.html 20050127.html 20050126.html 20050125.html 20050124.html 20050123.html 20050122.html 20050121.html 20051124.html 20050913.html 20050130.html 20050129.html
As you can see they are not sorted. Anyone has a clue why?

Yours desperatly,


holli, /regexed monk/


Update:
I have just noticed that all is well when I just use
[% FOREACH file = dir.files %]
Don't you call that premature optimization? ;(

Anyway the question is still valid. Why does the sort not work?

Replies are listed 'Best First'.
Re: Sort in Template Toolkit broken?
by davorg (Chancellor) on Nov 24, 2005 at 17:04 UTC
    [% FOREACH file = dir.files.sort %]

    Shouldn't that be

    [% FOREACH file = dir.files.sort('name') %]
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I've just tried that. It's still unsorted.


      holli, /regexed monk/
Re: Sort in Template Toolkit broken?
by Anonymous Monk on Nov 25, 2005 at 14:06 UTC
    If it still doesn't work, then its a bug