my $width = 10; # must be at least as big as the longest field being compared my @sorted = map substr($_, index($_, '|')+1), sort map sprintf("%0${width}d|%s", substr($_,rindex($_,'|')+1), $_), @database;