foreach my $loc (sort {$b->{users} <=> $a->{users}} @locations){ ... }