I have some Excel (.xlsx) files in a folder and its subfolders that I'm trying to list on my website. The file names have the following format:
2018_MyData_Day.xlsx
2018_MyData_Month.xlsx
2018_MyData_Year.xlsx
2019_MyData_Day.xlsx
2019_MyData_Month.xlsx
2019_MyData_Year.xlsx
Note: Day, Month, and Year are text, not a placeholder for an actual numeric day, month, or year (in case that was misleading).
I need to display these file names with a descending file name, but grouped by Year, Day, or Month in that specific order. So, the output, using the list above, should be:
2019_MyData_Year.xlsx
2018_MyData_Year.xlsx
2019_MyData_Day.xlsx
2018_MyData_Day.xlsx
2019_MyData_Month.xlsx
2018_MyData_Month.xlsx
I used the following to sort the file name by year and by the Day, Month, Year substring, but the substring is not ordered correctly:
var fileGroup = (
from file in Directory.EnumerateFiles(myPath, searchPattern: "*.xlsx", searchOption: SearchOption.AllDirectories)
let fileName = Path.GetFileName(file)
orderby fileName descending
select fileName
).OrderBy(f => f.Substring(f.LastIndexOf("_")))
I'm sure I need a condition since the desired order is custom, but I'm not sure how to implement it.
Is this even possible or is there a better way to achieve my desired output?
Thanks!