[英]c# list of strings > sort by regex ?
我對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??? )
謝謝你的建議。
Sort
方法適用於您的情況,但是由於基於文檔它並不穩定(在日期相似的情況下不保留原始順序),我建議使用OrderBy(穩定):
filteredLogFileC1 = filteredLogFileC1.OrderBy(dt => dt).ToList();
在上面的lambda (dt => dt)
,您說的是:按字符串自己的值對它們進行排序。
如果不是字符串,而是具有Date
字段的數據結構,則可以說( dt => dt.Date
)以便對該字段進行排序(只是清除可能會使您感到困惑的lambda)一點)。
我之前嘗試過,但沒有成功:
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));
如果我正確理解您的任務-可以是:
filteredLogFileC1.Union(filteredLogFileC2).OrderBy(l => sortReg.Match(l).Value)
代碼結果是IEnumerable。 您可以使用擴展方法.ToList()
進行轉換。 另外,如果Regex不匹配-結果值將為空字符串,否則將為所需的子字符串。
您應該從字符串創建日期,然后按日期排序
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.