my @records; while () { # Write a parse_record subroutine to return the variables you want my ($NUM, $SEC, $LONG, $LAT) = parse_record($_); # Stick your variables into an array for later use push @records, [ $NUM, $SEC, $LONG, $LAT ]; } #### # Sort by LONG (index 0=NUM, 1=SEC, 2=LONG, 3=LAT) my @sorted_recs = sort { $$records[$a][2] <=> $$records[$b][2] } @records; #### for my $rec (@sorted_recs) { # pull the variables from the current record my ($NUM, $SEC, $LONG, $LAT) = @$rec; print "Num=$NUM, Sec=$SEC, Long=$LONG, Lat=$LAT\n"; }