in reply to Store operation in a variable

Rather than burdening every comparison of the sort with a conditional test, it'll be more efficient to sort the keys to an array and then reverse the array conditionally:

my @sorted = sort { $scores{$b} <=> $scores{$a} } keys %scores; @sorted = reverse @sorted if condition; for ( @sorted ) { ... }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy