How do I sort a file-listing?

I have a script that uses:
  while ($filename = readdir DIR) {
to display a directory's contents. Is there anyway to sort these results in a particular order (i.e. by date?)
drummondAsked:
Who is Participating?
 
ozoCommented:
sub ST(&@){
        my $metric=shift;
        map {$_->[0]}
        sort {$a->[1] cmp $b->[1]}
        map {[$_,&{$metric}]} @_
}

for( ST{ ~(stat"$some_dir/$_")[9] } readdir DIR ){ }
0
 
ozoCommented:
sub ST(&@){
         my $metric=shift;
         map {$_->[0]}
         sort {$a->[1] cmp $b->[1]}
         map {[$_,&{$metric}]} @_
}

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
for $filename ( ST{(stat"$some_dir/$_")[9]} readdir DIR ){
      print $filename." ".localtime((stat"$some_dir/$filename")[9])."\n"
}

0
 
drummondAuthor Commented:
That made it work REALLY well. Now how do I reverse it so that the most recent files are at the top of the list?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ozoCommented:
for ( reverse ST{(stat"$some_dir/$_")[9]} readdir DIR ){
}
#or
for ( ST{ ~(stat"$some_dir/$_")[9] } readdir DIR ){
}
0
 
drummondAuthor Commented:
Everything works perfectly! Now how do I give you the points?
0
 
WileyKatCommented:
to get the points, ozo should post his answer as an answer instead of a comment. you can then give him the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.