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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: sorting files by date and names
by broquaint (Abbot) on Oct 11, 2002 at 12:41 UTC
    This depends on how you're iterating through your files. But under the assumption you've got a list of strings which refer to file names you could do the following
    my @sorted_files = sort map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, [stat]->[10] ] } @files;
    The main problem with this however is that ctime is not a true indication of the creation of the file (see. this node for more info).
    HTH

    _________
    broquaint

      How would you modify that to sort by creation time and then by modification time?

        Well, [stat]->[10] gives the "creation" time, and the modification time is in 9; so you could do this:

        my @sorted_files = map { $_->[0] } sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } map { [ $_, [stat]->[10], [stat]->[9] ] } @files;

        But for some reason, I'm tempted to do this:

        my @sorted_files = map { $_->[0] } sort { $a->[11] <=> $b->[11] or $a->[10] <=> $b->[10] } map { [ $_, stat ] } @files;
        We're building the house of the future together.
Re: sorting files by date and names
by mce (Curate) on Oct 11, 2002 at 12:38 UTC
    hi,

    This should do the trick:

    perl -e 'print join("\n",sort {(stat($a))[9] cmp (stat($b))[9] or $a c +mp $b } <*>)';
    Explanation:
    in sort, cmp is used with $a and $b, and can be or'ed.

    <*> gives all files in the current directory.

    I hope this helps. Try this
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

Re: sorting files by date and names
by zigdon (Deacon) on Oct 11, 2002 at 12:32 UTC