# comparison routine for sort() assumes (for fun) that col1 and col3 are # alpha, and col2 is numeric. Obviously this would have to reflect the data sub by_first_3_cols { $a->[0] cmp $b->[0] || # first, compare col1 $a->[1] <=> $b->[1] || # if equal, compare col2 $a->[2] cmp $b->[2]; # if equal, compare col3 } # sort list of arrayrefs, each containing data from one line of input my @sorted = sort by_first_3_cols map { chomp; my @columns = split /,/; [ @columns ] } ; # now print sorted list in CSV format (or whatever) print join( ',', @$_), "\n" for @sorted;