in reply to sort by field

tsvi,
Can this be doen in perl?

Here are a couple of Perl's mottos:

In this particular case, I would suggest considering changing your data structure. Depending on your situation, you may want an Array of Arrays (AoA) or even an Array of Hashes (AoH). Here are a couple of examples:
# AoA sorted by first name my @names = ( [ qw( David Gets ) ], [ qw( Richard Storm ) ], ); my @sorted = sort { $a->[0] cmp $b->[0] } @names; # AoH sorted by last name my @names = ( { fn => 'David', ln => 'Gets' }, { fn => 'Richard', ln => 'Storm' }, ); my @sorted = sort { $a->{ln} cmp $b->{ln} } @names;

Now there are more efficient ways to do this sort and I will leave that up to you - take a look at Super Search. I will be using Schwartzian Transform to answer the question you actually asked.

# Sorted by first name my @names = qw( David|Gets Richard|Storm ); my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, split /\|/, $_, 2 ] } @names;

Cheers - L~R