my @aRecords; my $iRecord=0; while ($line = ) { my @aFields = split(/,/, $line); $aRecords[$iRecord] = \@aFields; # \@ creates an array reference $iRecord++; } # now that you have read in all the lines, you can sort them # assuming age is the second field, it will be stored at # index 1 (fields are numbered starting at 0) my @aSortedRecords = sort { $a->[1] <=> $b->[1] } @aRecords;