所以我想按降序對文件進行排序。 文本文件看起來是這樣的:無法使用OrderBy或Sort排序列表
%[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] [extension = 21775]%
基本上我希望最終結果只輸出時間戳的唯一值。我已經使用子擺脫過量的文字,並輸出細,如下所示:
[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();
}
}
}
爲什麼你會發布此,如果你連承認它不是一個答案? –
@DangerZone因爲它太長,不能評論,我覺得OP和未來的讀者可以從中受益。 – Rotem
但是OP從文件中讀取其他內容。 – juharr