my @array1 = sort { $queryterms{$b} <=> $queryterm{$a} || $a cmp $b } keys %queryterms; my @array2 = map { $queryterms{$_} } @array1;