http://www.perlmonks.org?node_id=962180


in reply to Sort array + keep associated indexes in other array

You can compute an appropriately sorted list of indices, and then reorder both arrays using those indices:

#!/usr/bin/perl -l my @array_items = ("dog", "desk" ,"cow"); my @array_items_weight = ("20" , "10", "150"); my @i = sort { $array_items[$a] cmp $array_items[$b] } 0..$#array_item +s; @array_items = @array_items[@i]; @array_items_weight = @array_items_weight[@i]; print "@array_items"; # cow desk dog print "@array_items_weight"; # 150 10 20