my csv files have 14 columns and ~800.000 lines. I must sort csv orderby 10th column thenby 3rd column. I use below code but sorts by only 10th column
string filePath = "D:\\csv.csv";
string[] lines = File.ReadAllLines(filePath, Encoding.Default);
var data = lines.Skip(1);
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).Select(x => x.Line);
File.WriteAllLines("D:\\sortedCsv.csv", lines.Take(1).Concat(sorted), Encoding.Default);
my csv likes
- col1 ; col2 ; col3 ;......; col10; ..
- abc ; fds ;123456 ;.... ;123 ; ..
- def ; dsa ; 12435 ; .... 124 ; ..