I have an array of FileInfo objects with duplicate elements I'd like to filter, i.e.e remove duplicates, the elements are sorted by last write time using a custom comparer. The format of the file names is as follows:
file{number}{YYYMMDD}{HHMMSS}.txt
What I'd like to know is if there's an elegant way of filtering out two files with the same file number so that only the most recent is present in the list, i.e. I have two elements in my array with the following file names:
file1_20110214_090020.txt
file1_20101214_090020.txt
I would like to keep the most recent version of file1. The code I have for getting the files is as follows:
FileInfo[] listOfFiles = diSearch.GetFiles(fileSearch);
IComparer compare = new FileComparer(FileComparer.CompareBy.LastWriteTime);
Array.Sort(listOfFiles, compare);
Thanks for your help.
UPDATE:
Forgot to add the caveat, the program in question is using .Net 2.0, so no LINQ unfortunately. Sorry for the confusion, above I corrected the file number to be the same