SPUG: Sorting Files by Mod. Times

Scott Blachowicz sab at seanet.com
Wed Oct 6 12:51:23 CDT 1999


> You asked for alphabetic sorting when you said "sort {$a cmp $b}" ;
> I think what you want is "sort  { (stat $a)[9] <=> (stat $b)[9]; }",
> so you can compare the files by their modification times (returned
> by the stat function).

I wonder if there'd be a big performance hit to doing that many 'stat' calls?
Probably depends on how many files and how many times you sort them and how
far out of order they are to start with, but maybe it'd be worth caching the
mod times before sorting so you only stat each file once.

	%mod_time = ();
	@links = sort {
	 ($mod_time{$a} || ($mod_time{$a} = (stat $a)[9])) <=>
	 ($mod_time{$b} || ($mod_time{$b} = (stat $b)[9]));
      } @links;

> WARNING:  I just made this up, and didn't test it!

Ditto!

--
Scott.Blachowicz at seaslug.org


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list