David Green David Green - 24 days ago 5x
C# Question

Sorting/Grouping Items In A list

I have a list of filenames that will look something like this:





The commonalities will be the 'SNcp' pattern, where N is a number. what's before and after that may differ. It is machine generated output from a network switch. I can have 1, 2 or more in the list. I can handle 1 or 2. Either means they are from the same switch. Files from the same switch will match before and after the 'SNcp'. If there are more than 2 then it means logs from multiple switches are mixed in the same folder. The above example there are 2 switches. I need to be able to sort/and/or group by what comes before the 'SNcp' pattern. I think I need a regex to do it, but when I search for grouping lists by regex I get how to do named groups. So I'm not sure what the predicate should be for a GroupBy. The starting position of the 'SNcp' pattern will also vary, so I'm not sure how I would do a Substring.


So, in order to do this reliably, you'll need to manipulate the fact that you do, in fact, know that there's a pattern and reason to the various things. I'll assume the sort here is A-Z descending. Have some general algorithm:

Create a Dictionary<String, List<File>>.
   collect the first substring (0 to the first -)
   try to add the file to the dictionary using the substring as a key
      if it works, go to the next one
   Catch if the Key doesn't exist
      add the substring to the dictionary as a key with a new list
      add the file to the dictionary using the new key.
FOREACH key in the dictionary
  sort the list alphabetically

So, dictionaries are really good at grouping things by a commonality, which you've made apparent is either the Switch key (SNc) or the name to the left of it (SAN3000A and the like). We can do this because also there is a common delimiter (-) for all the potential files, so I can reliably 'guess' where my key is coming from.