I have a control which shows a list of all PDFs in a selected folder. The names of most of these PDFs (for example, meeting minutes) begin with a date. I want these PDFs shown in descending order so that the most recent PDFs are shown at the top.
In the same folder I also have some PDFs whose names do not contain a date (for example, policies). I want these files to be ordered alphabetically.
Here is an example of the kind of list I have and how I want it to be ordered:
- 2019-01-12 Meeting minutes.pdf
- 2018-11-19 Meeting agenda.pdf
- 2018-06-02 Meeting minutes.pdf
- 2017-12-13 Meeting agenda.pdf
- 2017-04-27 Meeting minutes.pdf
- Privacy policy.pdf
- Safeguarding policy.pdf
- Welfare policy.pdf
And here is what I have tried:
private void GenerateFolder()
{
folder_path = Server.MapPath(BaseFolder);
_folder_view = new StringBuilder();
if (Directory.Exists(folder_path))
{
DirectoryInfo info = new DirectoryInfo(folder_path);
FileInfo[] files = info.GetFiles().OrderByDescending(p => p.FullName).ToArray();
foreach (FileInfo file in files)
{
doStuff();
}
}
folder_view.Text = _folder_view.ToString();
}
With this code, the PDFs whose names begin with a date are ordered in descending order (by date) which is what I want. But the other PDFs whose names do not begin with a date are not ordered the way I would like (alphabetically). Is there a way to accomplish my dual-sorting objective?