in reply to Sorting by the value of the inner hash

Is $keyscore inside of the by_score subroutine the same as the one from your loop? At guess, I'd say it's not. Try putting the subroutine inline:

foreach $keyscore (sort { $totalscoreinv{$keyscore}{$b}<=>$totalscorei +nv{$keyscore}{$a} } keys %totalscoreinv) { ... }