my @output; while () { push @output, [ split(',', $_, 4) ]; } my @sorted = sort { $a->[0] cmp $b->[0] || $a->[3] <=> $b->[3] } @output;