如何在c#中对具有两列的csv文件进行排序 [英] how to sort csv file with two columns in c#

查看:354
本文介绍了如何在c#中对具有两列的csv文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的csv文件有14列和〜800.000行.我必须按第10列然后按第3列对csv order进行排序. 我使用以下代码,但仅按第10列排序

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);

我的csv喜欢

  • col1; col2; col3; ......; col10; ..
  • abc; fds; 123456; ....; 123; ..
  • def; dsa; 12435; .... 124; ..

推荐答案

var sorted = data.Select(line => new
{
    SortKey = Int32.Parse(line.Split(';')[9]),
    SortKeyThenBy = Int32.Parse(line.Split(';')[2]),
    Line = line
}
).OrderBy(x => x.SortKey).ThenBy(x => x.SortKeyThenBy)

这篇关于如何在c#中对具有两列的csv文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆