foreach $line (sort { (split /:/, $a)[0] <=> (split /:/, $b)[0] } @list){ ...