简体   繁体   中英

c# list of strings > sort by regex ?

I am quite new to c# but I can't resolve this issue ( most probably a simple one ).

I have 2 List that contain strings of error logs. ( Let me know if it's better to use an array of strings )

/* 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??? )

Thank you for your suggestions.

Sort method would work for your case, but since it is not stable based on the docs (doesn't preserve original order in case of similar dates), I Recommend using OrderBy (which is stable):

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

In the above lambda (dt => dt) , you're saying: Sort the strings by their own values.

If that wasn't a string but a data structure that have a Date Field for example, you could say ( dt => dt.Date ) in order to sort by that field (just to clear the lambda which seems to be confusing you a little bit).

I tried this before and didn't work:

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

This way it works for me and generates the output also:

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

If I understood your task right - this can be:

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

The code result is IEnumerable. You can use an extension method .ToList() to converti it. Also, if Regex fails to match - the result value will be an empty string else it will be the desired substring.

You should create date from your string, and sort by date

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM