This is not too shabby to start with, you're on the right track. To sort by the day of the week, use a hash as a lookup, to retrieve the day value:

my %dow = (qw( Sunday 0 Monday 1 Tuesday 2 Wednesday 3 Thursday 4 Friday 5 Saturday 6 )); # ... $dow{$a[7]} <=> $dow{$b[7]}

You might want to make Sunday => 7 if you want it to sort higher, to put the weekend at the end. I'm using a qw() quote-word construct, which returns a list and assigns that to a hash. Just a bit less typing.

For the timestamps, if you can guarantee that they will always follow a ddd:dd:dd format, you can just sort them alphabetically as you are already doing

To combine them all into an efficient sort statement, use a Guttman-Rosler Transform (GRT)*. The idea is to build a prefix to the string so that you can use a bare sort operation, and then throw away the prefix after the sort and recover the original record.

my @out = map { (split /%/)[2] } sort map { my @field = split /,/; "$dow{$field[7]}%$field[8]%$_" } @in ;

Since the first field of the record is also part of the sort criteria, you don't actually have to make it part of the prefix. So you only have to add on a munged day of week and the timestamp to have it sort correctly. Then, after the sort, split it up on % and throw the first two away to recover your record.

If the array can be really large, say, over 200_000 entries, then you should consider preparing data files so that the operating system sort command can be used. Otherwise I'd just stick with Perl.

* see Advanced Sorting - GRT - Guttman Rosler Transform for a discussion on the GRT.

- another intruder with the mooring in the heart of the Perl


In reply to Re: Sort questions (use a GRT) by grinder
in thread Sort questions by wube

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.