in reply to sort by field
Here are a couple of Perl's mottos:
# 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
|
|---|