in reply to Advanced Sorting

In that case, the only optimization to your code I can think of is:
if ($field eq "name") { $a->{$field} cmp $b->{$field} } else { $a->{$field} <=> $b->{$field} }
which stops the code from examining every value twice, but simply evaluates a scalar. It benchmarks as slightly faster. Actually, for ultimate speed, move the field check before the sort subroutine is called:
if ($field eq "name") { @sorted = sort @data; } else { @sorted = sort {$a <=> $b} @data; }
etc...

Replies are listed 'Best First'.
RE: Re: Advanced Sorting
by Anonymous Monk on Oct 28, 2000 at 06:02 UTC
    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?
      1. read the perlman (or unixman) for the commands stat() and sort()
      2. try to understand the following :-)
      sub bydate{(stat $dir.$a)[9]<=>(stat $dir.$b)[9];} @sortedfiles = sort bydate @files_in_your_dir;
      Have a nice day
      All decision is left to your taste
      Update
      ok, I might have been wrong when assuming that logs get analyzed but not changed, after they have been archived