in reply to How do you sort an array?

You should write a custom sort routine which will work with your data, and you do this by using the optional sort() function parameter. This can enable sort() to order your data not by the value of the data itself, but by some calculated value derived from it.
sub by_time { # $a and $b are things to be compared from sort() # Convert $a and $b into integer values # Extract the time from the string into $a_v my ($a_v) = $a =~ / (\d\d?:\d\d:\d\d)/; # Remove colons to create a numeric-only value $a_v =~ tr/://d; # Same for $b my ($b_v) = $b =~ / (\d\d?:\d\d:\d\d)/; $b_v =~ tr/://d; # Return the difference (0 = same) return $a - $b; } foreach (sort by_time @stuff) { print "$_\n"; }

Replies are listed 'Best First'.
Re: Re: How do you sort an array?
by Anonymous Monk on Mar 24, 2001 at 01:41 UTC
    Thanks!