sub f{%h=@_;for(values %h){for(@$_){$h{$_}||=[]}} sort{(grep /^$b$/,@{$h{$a}})?1:-1}keys%h}