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"; }