我对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???)
谢谢您的建议。
有'.AddRange'方法将第二列表添加到第一个。 –
“按正则表达式排序”是什么意思?'最终结果应该是什么? –
如果你的日期总是相同的格式(并且总是从年份到毫秒),你应该可以使用标准的字母排序。 –