foreach my $this_record (sort byname @record_set) { # do something } sub byname { $a->{'NAME'} cmp $b->{'NAME'} }