C# 如何在c中用两列对csv文件进行排序#
我的csv文件有14列和大约800.000行。我必须先按第10列排序,然后按第3列排序。 我使用下面的代码,但只按第10列排序C# 如何在c中用两列对csv文件进行排序#,c#,linq,sorting,csv,C#,Linq,Sorting,Csv,我的csv文件有14列和大约800.000行。我必须先按第10列排序,然后按第3列排序。 我使用下面的代码,但只按第10列排序 string filePath = "D:\\csv.csv"; string[] lines = File.ReadAllLines(filePath, Encoding.Default); var data = lines.Skip(1); var sorted =
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);
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
我的csv喜欢
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
- col1;col2;col3;。。。。。。;col10强>
- abc;fds;123456 ;.... ;123 ; ..李>
- def;dsa;12435 ; .... 124 ; ..李>
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
试试这个:
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
SortKey2 = line.Split(';')[2],
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x=>x.SortKey2).Select(x => x.Line);
基本上,添加第二个排序标准,然后按指定的顺序排序。您必须使用
OrderBy(…)。然后按(…)
:
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.Line);
请注意,在这种情况下,File.ReadLines
比File.ReadAllLines
更有效
var sorted = data.Select(line => new
{
SortKey = Int32.Parse(line.Split(';')[9]),
Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.Line);