http://www.perlmonks.org?node_id=38899

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (sorting)

I am just getting into perl,and have a basic simple question. I have written a script that reads in a directory of logfiles, sorts them in reverse chronological order, and outputs them into an html page.

I cannot figure out how to sort the directory deeper than just the month, which comes first (the files in the dir are all listed by month/day/year). How can I sort this so the most recent days are first?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: how do I sort a list chronologically from most recent to last?
by chromatic (Archbishop) on Oct 28, 2000 at 06:19 UTC
    This is in perlfaq4, specifically How do I sort an array by (anything)?

    In this case, you should probably look into making a more advanced data structure with split or using substr in a more complex sort function. Something like this may work:

    my @sorted = sort { substr($a, 0, 4) <=> substr($b, 0, 4) } @unsorted;

    This code is untested, suitable only as an example of technique, not syntax.

Re: how do I sort a list chronologically from most recent to last?
by ashok (Sexton) on Nov 03, 2000 at 01:30 UTC
    You can use shell commands to get the files in chronological order i.e by time stamp and copy those into an array. I assume that only log files are stored in that directory.
    @files = `ls -lt | tr -s " " | cut -f9 -d' '`; #@files finally contains files most recent are first
Re: how do I sort a list chronologically from most recent to last?
by I0 (Priest) on Dec 26, 2000 at 21:40 UTC
    @sorted = map {join'/',(split/-/)[1,2,0]} sort map {join'-',(split'/')[2,0,1]} qw(12/31/2000 12/26/2000 01/01/2001);