0

我正在尝试使用制表符分隔的 txt 文件中的第三列对数据进行排序。尝试了几种方法,不确定我如何使用第三列对其进行排序。目前已经使用第一个对其进行了排序。此外,我需要从第 3 列中删除重复项(区分大小写,即 river 与 River 不同)这是我的代码。一旦我取得进展,将标记为答案。谢谢 ;)

string[] lines = File.ReadAllLines(@"d:\instance_test.txt");
//Dictionary<String, Int32> EAR_appcode = new Dictionary<String, Int32>();
//Console.WriteLine();
//Console.ReadLine();
//// Display the file contents by using a foreach loop.
//System.Console.WriteLine("Contents of WriteLines2.txt = ");
//foreach (string line in lines)
//{
//    // Use a tab to indent each line of the file.
//    Console.WriteLine("\t" + line.Substring(4));
//    Console.ReadLine();
//}
var no = lines;

var orderedScores = lines.OrderBy(x => x.Split(' ')[0]);
//string result = Regex.Split(no, @"[,\t ]+");
foreach (var score in orderedScores)
{
    string replacement = Regex.Replace(score, @"\t|\n|\r", "           ");
    DataTable table = new DataTable();
    table.Columns.Add("myCol", typeof(string));
    table.Columns.Add("myCol2", typeof(string));
    table.Columns.Add("EAR_appcode", typeof(string));
    table.Rows.Add(11, "abc11");
    table.Rows.Add(13, "abc13");
    table.Rows.Add(12, "abc12");
    Console.WriteLine(replacement) ;
    Console.ReadLine();

}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();

}
4

2 回答 2

0

就像是:

// read lines somehow
// ...
// create a list
var list = new List<Tuple<string, string, string>>();
foreach(string line in lines)
{
    var split = line.Split('\x9');
    list.Add(new Tuple(split[0], split[1], split[2]));
}
// sort
list = list.OrderBy(x => x.Item3);
// remove duplicates
for(int i = 1; i < list.Count; i++)
    if(list[i].Item3 == list[i-1].Item3)
        list.RemoveAt(i);

我相信只需一个 linq 表达式就可以完成上述所有操作,但我在这方面做得很糟糕。无论如何都要从你那里偷OrderBy一部分^^。

如果您没有 .Net Framework 4.0,则替换Tuple为非通用版本(将列表声明为List<Tuple>):

class Tuple
{
    public string Item1;
    public string Item2;
    public string Item3;
    public Tuple(string i1, string i2, string i3)
    {
        Item1 = i1;
        Item2 = i2;
        Item3 = i3;
    }
}
于 2014-01-13T14:07:14.623 回答
0

这是我的示例数据:

Col1    Col2    Col3
zxcv    789 14:02
asdf    123 12:00
qwer    456 13:01
asdf    123 12:00

我使用这个 LINQ 语句来:

  1. 创建从“start”到“lines.Length - 1”的索引范围
  2. 由 '\t' 分割
  3. 将每一列转储为匿名类型
  4. 按字符串分组,即所有列的组合
  5. 仅选择每个组的第一项
  6. 按第 3 列排序

    static void Main(string[] args)
    {
        string[] lines = File.ReadAllLines("Tab.txt");
        int start = 1; // set to zero, if no header
    
        var records = (from i in Enumerable.Range(start, lines.Length - 1)
                       let pieces = lines[i].Split('\t')
                       select new { Col1 = pieces[0], Col2 = pieces[1], Col3 = pieces[2] })
                       .GroupBy(c => c.Col1 + c.Col2 + c.Col3)
                       .Select(gr => gr.First())
                       .OrderBy(c => c.Col3);
    
        foreach (var r in records)
            Console.WriteLine("{0}, {1}, {2}", r.Col1, r.Col2, r.Col3);
    
        Console.WriteLine();
        Console.WriteLine("Done");
        Console.ReadLine();
    }
    

当然,您可以在 LINQ 语句的最后一行添加解析/转换代码以按 int 或 DateTime 排序。

我测试了它...

于 2014-01-13T14:08:35.230 回答