sub sort_items { my $items = shift; my @items_sorted = sort { ( $a->{'id'} || 9999999999999 ) <=> ( $b->{'id'} || 9999999999999 ) || ( psql_date_to_DateTime( $a->{'dateend'} ) <=> psql_date_to_DateTime( $b->{'dateend'} ) ) } @$items; \@items_sorted; } # later ... sub foo { my $items = shift; # array ref $items = sort_items( $items ); }