foreach(@csvfiles) { $csvfile=$_; open(hanr,"D:\\stock\\".$csvfile) or die"error read $!\n"; open(hanw,">D:\\stock\\sorted".$csvfile) or die"error write $!\n"; @lines = (); @lines = ; shift(@lines);#remove header csv @sorted = sort multisort @lines; sub multisort { ($field1a, $field2a, $field3a)=split(/\,/, $a); ($field1b, $field2b, $field3b)=split(/\,/, $b); $field1a cmp $field1b or $field2a cmp $field2b; } print hanw @sorted; close(hanw); close(hanr); }