无法使用OrderBy或Sort

时间:2015-10-08 16:03:18

标签: c# sorting

所以我试图按降序排序文件。 文本文件如下所示:

%[TIMESTAMP = 1441737006376] [EVENT = agentStateEvent] [队列= 79651] [AGENTID = 61871] [延长= 22801] [状态= 2] [原因= 0]%

%[TIMESTAMP = 1441737006102] [EVENT = agentStateEvent] [队列= 79654] [AGENTID = 62278] [延长= 22828] [状态= 2] [原因= 0]%

%[TIMESTAMP = 1441737006105] [EVENT = CallControlTerminalConnectionTalking] [CALLID = 2619] [UCID = 10000026191441907765] [设备类型= 1] [DEVICENAME = 21775] [队列=] [中继线= 384:82] [TrunkType = 1] [TrunkState = 1] [原因= 100] [CalledDeviceID = 07956679058] [CallingDeviceID = 21775] [延长= 21775]%

基本上我希望最终结果只输出时间戳的唯一值。我已经使用substring来摆脱多余的文本,并输出正常,如下所示:

[TIMESTAMP = 1441737006376]

[TIMESTAMP = 1441737006102]

[TIMESTAMP = 1441737006105]

然而我希望它按以下顺序排序(基本上数字降序为升序):

[TIMESTAMP = 1441737006102]

[TIMESTAMP = 1441737006105]

[TIMESTAMP = 1441737006376]

我尝试了.sort和.orderBy但没有任何快乐。在使用任何子字符串格式之前我会使用这个就足够了,但显然不是。

代码如下:

   using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FedSorter
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            string line;
            string readIn = "C:\\Users\\xxx\\Desktop\\Files\\ex1.txt";
            System.IO.TextWriter writeOut = new StreamWriter("C:\\Users\\xxx\\Desktop\\Files\\ex1_new.txt");
            List<String> list = new List<String>();

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(readIn);
            string contents = "";
            string checkValues = "";
            while ((line = file.ReadLine()) != null)
            {
                string text = line;
                text = text.Substring(1, 25);
                if (!checkValues.Contains(text))
                {
                    list.Add(text);
                    Console.WriteLine(text);
                    writeOut.WriteLine(text);
                    counter++;
                }

                contents = text;
                checkValues += contents + ",";
            }
            list = list.OrderBy(x => x).ToList();
            writeOut.Close();
            file.Close();
            orderingFile();

        }
        public static void orderingFile()
        {
            string line = "";
            string readIn = "C:\\Users\\xxx\\Desktop\\Files\\ex1_new.txt";
            System.IO.TextWriter writeOut = new StreamWriter("C:\\Users\\xxx\\Desktop\\Files\\ex1_new2.txt");
            List<String> ordering = new List<String>();
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(readIn);
            while ((line = file.ReadLine()) != null)
            {
                ordering.OrderBy(x => x).ToList();
                ordering.Add(line);
                writeOut.WriteLine(line);
            }
            writeOut.Close();
            file.Close();
        }


    }
}

2 个答案:

答案 0 :(得分:2)

您正在创建新列表,您需要将其分配给变量

list = list.OrderBy(x => x).ToList();

但是,在创建和排序后,您甚至看起来甚至不会使用list。此外,{/ 1}}方法中的问题与

相同
orderingFile

但是,不是在每一行上排序和创建新列表,最好使用SortedList<TKey, TValue>,以便在添加内容时对内容进行排序。

但是,在ordering.OrderBy(x => x).ToList(); 中完成添加后,您实际上并没有使用ordering列表。如果您想要读取文件中的值,对它们进行排序然后将它们输出到另一个文件,那么您需要按顺序执行它。

答案 1 :(得分:0)

除了@ juharr的正确答案之外,你最好利用LINQ来大大简化你的代码。

string readIn = "C:\\Users\\xxx\\Desktop\\Files\\ex1.txt";
var timestamps = File.ReadAllLines(readIn)
                     .Select(l => l.Substring(1, 25))
                     .Distinct()
                     .OrderBy(t => t)
                     .ToArray();

要写出这些值,您可以使用foreach上的timestamps并将每行写出来TextWriter,也可以再次使用File课程:

string readOut = "C:\\Users\\xxx\\Desktop\\Files\\ex1_new.txt";
File.WriteAllLines(readOut, timestamps); 
//notice I've changed it to ToArray in the first part instead of ToList.