我對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'方法將第二列表添加到第一個。 –
「按正則表達式排序」是什麼意思?'最終結果應該是什麼? –
如果你的日期總是相同的格式(並且總是從年份到毫秒),你應該可以使用標準的字母排序。 –