2015-10-08 53 views
0

所以我想按降序對文件進行排序。 文本文件看起來是這樣的:無法使用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(); 
     } 


    } 
} 

回答

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

寫出來的值,您可以使用上timestamps一個foreach並在每行寫出來給你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. 
+0

爲什麼你會發布此,如果你連承認它不是一個答案? –

+0

@DangerZone因爲它太長,不能評論,我覺得OP和未來的讀者可以從中受益。 – Rotem

+0

但是OP從文件中讀取其他內容。 – juharr

2

您正在創建一個新的列表,你需要將其分配給變量

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

然而,它並不像你甚至可以使用list後你創建並對其進行排序。另外,你必須在orderingFile方法相同的問題與

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

但是代替整理和創造,每行一個新的列表會更好使用SortedList<TKey, TValue>將保持排序的內容添加到它的。

但是,在foreach中添加到ordering列表後,實際上並未使用ordering列表。如果您正在閱讀文件中的值,對它們進行排序,然後將它們輸出到另一個文件,那麼您需要按照該順序進行操作。

+0

我在調用此在正確的地方,因爲即時通訊仍然得到同樣的問題:( –

+0

伊夫添加了第二個方法只是重新讀取新的文件拿過來,試圖重新排序,但沒有喜悅 –

+0

這隻會由'list'變量引用的名單影響排序。你是否試圖重新排列寫入「ex 1_new.txt「文件?請注意,您與'ordering.OrderBy(x => x).ToList()'有同樣的問題。 – juharr