perl - sort an array according to elements of a second array -


suppose have 2 arrays this:

('1', '6', '8', '4', '5') ('a', 'c', 'd', 'f', 'w') 

i want sort first array, , order of elements in second array should change in same way first array, order of 2 becomes follows:

('1', '4', '5', '6', '8') ('a', 'f', 'w', 'c', 'd') 

any ideas of how in perl?

you need sort indices array. this

use strict; use warnings;  @aa = qw/ 1 6 8 4 5 /; @bb = qw/ c d f w /;  @idx = sort { $aa[$a] <=> $aa[$b] } 0 .. $#aa;  @aa = @aa[@idx]; @bb = @bb[@idx];  print "@aa\n"; print "@bb\n"; 

output

1 4 5 6 8 f w c d 

Comments