• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Sortation of many lists according the second field

I must sort many lists like
@l1 = ($a1,$b1,$c1,$d1);
@l2 = ($a2,$b2,$c2,$d2); ... @ln=($an,$bn,$cn,$dn);

The sortation of the lists  is done according of the second field of the lists. The order of the list should be
upstairs, example:
$b2 < $b1  =>  @l2 < @l1

1 Solution
sort { $a->[1]<=>$b->[1] }
Before using Ozo's solution, create an array whose elements are the references to your arrays @l1, @l2, ..., @ln.

# replace the ... with the actual array references below
@array_list = (\@l1, \@l2, ..., \@ln);

# sort the array list
@sorted_list = sort { $a->[1] <=> $b->[1] } @array_list;

# if the aray elements are strings, use 'cmp' instead of <=>
@sorted_list = sort { $a->[1] cmp $b->[1] } @array_list;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now