# this should be read from the last line to the top one my @sorted= map { substr( $_, 8) } # [3]: remove the date sort # [2]: sort aplphabetically on the date map { $_= @{[split(/\s/, $_)]}[3] . $_} # [1]: get the date and add it at the beginning of the string ; print join "\n", @sorted; __DATA__ text 20000407 text text 20000409 text text 20000408 text text 20000507 text