in reply to backwards if and while looping

Not really sure what you're trying to do, but 1) avoid $a and $b outside of sort subs and 2) if $a and $b are integers, you can simplify to:
$error or $a = $a < $b ? $b : $a;
cLive ;-)