# Place all the hash data into an array of pipe-seperated values for sorting foreach my $key (keys %test) { push @sorted, "$key|$test{$key}->[0]|$test{$key}->[1]|" . "$test{$key}->[2]|$test{$key}->[3]"; } # Sort the data by the number in the last part of the array. @sorted = map $_->[1], sort { $a->[0] <=> $b->[0] } map [ substr($_,rindex($_,'|')+1), $_ ], @sorted; # Put the line breaks back in. for (@sorted) { $_ = "$_\n"; }