sub by_dept_total { return $dept_n_code{$a}->{'dept'} <=> $dept_n_code{$b}->{'dept'}; } sort by_dept_total ...