foreach my $row ( sort { $a->{name} <=> $b->{name} } @sorted_data ) { #Move the values to second and remove the last one }