I have two string arrays, one is of all the calendar versions that the local computer has, the other is of all the versions that an ftp server has. I know how to fill both arrays, I just don't know how to compare them to look for versions that are out of date.
I need to look through the local computer array (strings look like: date1-date2-version e.g. 2021-2022-3), and return an array of strings of files that need to be updated.
A few situations that also need to be addressed by this code:
Example of ftp folders array:
Example of local folders array:
Example of results of versions that need to be updated (plus explanation):
Let's reduce the ftp folders to only the highest versions:
var maxFtps = ftpArray
.GroupBy(x => x[..9], x => int.Parse(x[10..]))
.Select(g => (Year: g.Key, Ver: g.Max()))
This pulls the first 9 chars off every string (so the year-year part) and also pulls for char 10 onwards and parses to int The entries are grouped on the years so your example will reduce to 3 entries and each grouping will be a sequence of ints which is the version
Next we select just the key and the max version int to produce some temporary ValieTuple objects like:
"2019-2020", 2
"2020-2021", 4
"2021-2022", 1
Now, we don't need to be so technical for the locals. Essentially we only need to look up if a Year prefix exists locally to know what to do with the ftp version
var localYears = localsArray.Select(x => x[..9]).ToHashSet();
We make a hashset to store the output, as it will automatically dedupe
var localHash = localsArray.ToHashSet();
Then we can make a decision on each of the max ftps- add it if it's known locally or if it's the most recent
foreach(var m in maxFtps)
{
var recomposed = m.Year + "-" + m.Ver;
//add it if its a known local year - hashset will auto dedupe
if(localYears.Contains(m.Year))
localHash.Add(recomposed);
//or if this ftp entry represent the most recent
if(recomposed == mostRecentCalender)
localHash.Add(mostRecentCalender);
}
I realized that you didn't specify what is to happen if there is eg a mostRecentCalender of "2021-2022-4", a local of "2021-2022-1" and an ftp of "2021-2022-2". The code as it stands will add the ftp version even though it's not the most recent. You might have to tweak the logic if you don't want to add