sub by_date { my ($monA, $monB, $dayA, $dayB, $yearA, $yearB); ($monA, $dayA, $yearA) = split(/\//, $a); ($monB, $dayB, $yearB) = split(/\//, $b); $yearA <=> $yearB or $monA <=> $monB or $dayA <=> $dayB } foreach $k (sort by_date %H) { ... }