2017-06-29 91 views
0

我对c#很新,但我无法解决这个问题(最可能是一个简单的问题)。c#字符串列表>按正则表达式排序?

我有2列表包含错误日志的字符串。 (让我知道是否最好使用一串字符串)

/* Example of list from host 1 
2017-06-29 02:25:54.309 BST,ERROR,....... 
2017-06-29 02:25:54.357 BST,ERROR,....... 
2017-06-29 02:25:54.495 BST,ERROR,....... 
2017-06-29 02:30:57.183 BST,ERROR,....... 
2017-06-29 03:07:12.078 BST,ERROR,....... 
2017-06-29 05:07:13.256 BST,ERROR,....... 
2017-06-29 05:14:14.717 BST,ERROR,....... 
2017-06-29 05:16:23.954 BST,ERROR,....... 
2017-06-29 08:12:16.418 BST,ERROR,....... 
2017-06-29 08:37:23.574 BST,ERROR,....... 
2017-06-29 09:07:11.569 BST,ERROR,....... */ 
List<string> filteredLogFileC1 = filterLog(hostNameC1); //filterLog returns a List<string> 

/* Example of list from host 2 
2017-06-29 00:43:43.781 BST,ERROR,....... 
2017-06-29 00:43:44.446 BST,ERROR,....... 
2017-06-29 00:43:44.885 BST,ERROR,....... 
2017-06-29 00:43:45.378 BST,ERROR,....... 
2017-06-29 00:43:45.940 BST,ERROR,....... 
2017-06-29 00:43:46.584 BST,ERROR,....... 
2017-06-29 00:43:47.141 BST,ERROR,....... */ 
List<string> filteredLogFileC2 = filterLog(hostNameC2); //filterLog returns a List<string> 

// Combine the 2 lists into one (the below practice might not be the best one but its working and I am happy at the moment :)) 

/* 
... Combined list 
2017-06-29 08:12:16.418 BST,ERROR,....... 
2017-06-29 08:37:23.574 BST,ERROR,....... 
2017-06-29 09:07:11.569 BST,ERROR,....... 
2017-06-29 00:43:43.781 BST,ERROR,....... 
2017-06-29 00:43:44.446 BST,ERROR,....... 
2017-06-29 00:43:44.885 BST,ERROR,....... 
... 
*/ 
foreach (string line in filteredLogFileC2) filteredLogFileC1.Add(line); 


// I need to sort the filteredLogFileC1 list by date. 
// Below I have a regex that I've put together but I don't know how I can use it 

Regex sortReg = new Regex(@"(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}.\d{3})"); 

Issue: filteredLogFileC1.OrderBy(???sortReg???) 

谢谢您的建议。

+0

有'.AddRange'方法将第二列表添加到第一个。 –

+1

“按正则表达式排序”是什么意思?'最终结果应该是什么? –

+0

如果你的日期总是相同的格式(并且总是从年份到毫秒),你应该可以使用标准的字母排序。 –

回答

0

Sort方法将工作你的情况,但因为它的基础上的文档并不稳定(不保留在类似日期的情况下,原来的顺序),我建议使用排序依据(这是稳定的):

filteredLogFileC1 = filteredLogFileC1.OrderBy(dt => dt).ToList(); 

在上面的lambda (dt => dt)中,你的意思是:按照自己的值排序字符串。

如果这不是一个字符串,而是一个数据结构,例如有一个Date字段,您可以说(dt => dt.Date)为了按该字段进行排序(只是为了清除lambda,这似乎让您感到困惑一点位)。

0

我尝试过这一点,并没有奏效:

filteredLogFileC1.OrderBy(x => x)); // maybe I should have stored this into a new list ? 
File.WriteAllLines(localPath + "combined.log", filteredLogFileC1); 

这样,它为我的作品,也产生输出:

File.WriteAllLines(localPath + "combined.log", filteredLogFileC1.OrderBy(x => x)); 
+0

'OrderBy'返回一个新的'IEnumerable',并不会更新您正在操作的那个。 – user3185569

0

如果我明白你的任务权 - 这可以是:

filteredLogFileC1.Union(filteredLogFileC2).OrderBy(l => sortReg.Match(l).Value) 

代码结果是IEnumerable的。您可以使用扩展方法.ToList()来转换它。此外,如果正则表达式不匹配 - 结果值将是一个空字符串,否则它将是所需的子字符串。

0

您应该从字符串创建日期,按日期排序

var matchedLines = filteredLogFileC1.Where(x => sortReg.IsMatch(x)).OrderBy(x => DateTime.ParseExact(sortReg.Match(x).Value, "yyyy-MM-dd HH:mm:ss.fff", null)); // lines that match date pattern, ordered by date value 
var unMatchedLines = filteredLogFileC1.Where(x => !sortReg.IsMatch(x)); // Lines that do not match date pattern. Can be added at the top or bottom 
+0

如果不匹配,您将得到一个例外 – Kantora

+0

我编辑了答案,因此当行与日期模式不匹配时,不会出现异常。 –