in reply to Sorted by date file not working?!

How do you populate the @filelist array?    Are you using readdir?

foreach $file (sort by_last_mod @filelist) {push (@sorted,$file);} could more simply be written as: push @sorted, sort by_last_mod @filelist;.

Replies are listed 'Best First'.
Re^2: Sorted by date file not working?!
by natol44 (Sexton) on Mar 04, 2010 at 10:54 UTC
    Yes I am using readdir.

    I checked that @filelist is correct (really contains the file names), and that it is not already sorted (LOL);

    After the sort, I check @sorted and see that it is exactly the same then @filelist. No sort at all was done.

    I also used File::Stat, same result :(

      If you are using readdir then you have to prepend the directory name to the file name when you do the stat, for example:

      opendir DH, $dir or die "Cannot opendir '$dir' $!"; my @filelist = readdir DH; my @sorted = sort by_last_mod @filelist; sub by_last_mod { my $adate = ( stat "$dir/$a" )[ 9 ]; my $bdate = ( stat "$dir/$b" )[ 9 ]; return $adate <=> $bdate; }