my $str = `type unsort.txt`; $str = join("\n", map($_->[0], sort( {$a->[1] <=> $b->[1] }#for alpha order change <=> to cmp map( [$_, (split('\|', $_))[1] ],#change this number to control which field to use, 0 for 1st field split(/\n/, $str) ) ) ) ); print $str;