use strict; use warnings; # initial data my @x = qw/c d e f k l m n/; my @y = qw/4 6 5 2 9 7 8 3/; # store in hash to keep association between x and y after sorting my %data; for my $i (0..@x-1) { $data{$i}{x} = $x[$i]; # store x corresponding to position $i $data{$i}{y} = $y[$i]; # store y corresponding to position $i } # sort positions with respect to y my @sorted = sort { $data{$a}{y} <=> $data{$b}{y} } keys %data; print "Sorted data:\n"; for my $i (@sorted) { print "$data{$i}{y} corresponds to $data{$i}{x} at position $i\n"; }