Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How do I sort a list chronologically from most recent to last?

( #38899=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Oct 28, 2000 at 06:04 UTC
Q&A  > sorting


Description:

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?

Answer: how do I sort a list chronologically from most recent to last?
contributed by chromatic

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.

Answer: how do I sort a list chronologically from most recent to last?
contributed by ashok

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
Answer: how do I sort a list chronologically from most recent to last?
contributed by I0

@sorted = map {join'/',(split/-/)[1,2,0]} sort map {join'-',(split'/')[2,0,1]} qw(12/31/2000 12/26/2000 01/01/2001);

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (9)
    As of 2014-10-02 08:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (52 votes), past polls