# in pseudo-code: for each element calculate the sorting value for that element put the original element and the sorting value into a 2-element list pass these 2-element lists to your sorting routine, which sorts based on the sorting value of each element for each element in the sorted list of 2-element lists pull out the original element # in perl, an example using the typical map/sort/map layout # to sort a list of numbers based on the return value of a # complex subroutine that calculates the number of primes # less than each number's 25th power: my @newarray = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_ => n_primes_less_than_25th_power($_) ] } @oldarray;