I have the following classes
public class NewsLetter
{
public string FileName { get; set; }
}
public class Folder
{
public Folder()
{
this.NewsLetter = new HashSet<NewsLetter>();
}
public string FolderName { get; set; }
public virtual ICollection<NewsLetter> NewsLetter { get; set;}
}
In my controller I have created a List<Folder> of all of the sub-directories inside the newsletter directory, directories are years starting in 2006 to present, and every file in each sub-directory. The files are all pdf documents starting with 2 digit month in the document name. I want to reverse the sort of the directories and the files so that the latest is always on top.
Here is my Controller code
public ActionResult Index()
{
try
{
string path = Server.MapPath("/Newsletters/");
string[] folderPaths = Directory.GetDirectories(path);
List<Folder> fldrs = new List<Folder>();
foreach(string folder in folderPaths)
{
Folder fldr = new Folder();
List<Newsletter> newsletters = new List<Newsletter>();
string thisFolder = Path.GetFileName(folder);
string[] filePaths = Directory.GetFiles(folder);
foreach(string newsletter in filePaths)
{
Newsletter nl = new Newsletter();
nl.FileName = Path.GetFileName(newsletter);
newsletters.Add(nl);
}
fldr.FolderName = thisFolder;
fldr.Newsletter = newsletters;
fldrs.Add(fldr);
//fldrs.Reverse();
}
return View(fldrs);
}
catch (Exception ex)
{
Errors.ErrorOccured(ex);
}
return RedirectToAction("Index", "Error", new { Area = "" });
}
That works fine and all years and the folders in them display in the view. However, they are displayed chronologically and I need to reverse that.
this is what I am seeing
2021
01-Jan.pdf
02-Feb.pdf
03-Mar.pdf
04-Apr.pdf
05-May.pdf
06-Jun.pdf
07-Jul.pdf
08-Aug.pdf
09-Sep.pdf
10-Oct.pdf
11-Nov.pdf
12-Dec.pdf
2022
01-Jan.pdf
...snipped for brevity
What I am having a problem with is in the reversing of the sort. If uncomment the commented out line fldrs.Reverse() I get the year reversed only for even number years. All of the odd number years are then listed in order. The files in them however, are still in chronological order. So how do I go about reversing the output to display as I want?
my desired eventual output would be
2022
11-Nov.pdf
10-Oct.pdf
...snipped for brevity
01-Jan.pdf
2021
12-Dec.pdf
...snipped for brevity
01-Jan.pdf
etc...
CodePudding user response:
It is necessary to reverse the folders list and files list separately:
public ActionResult Index()
{
try
{
string path = Server.MapPath("/Newsletters/");
string[] folderPaths = Directory.GetDirectories(path);
List<Folder> fldrs = new List<Folder>();
foreach(string folder in folderPaths.Reverse()) // <--- REVERS
{
Folder fldr = new Folder();
List<Newsletter> newsletters = new List<Newsletter>();
string thisFolder = Path.GetFileName(folder);
string[] filePaths = Directory.GetFiles(folder);
foreach(string newsletter in filePaths.Reverse()) // <--- REVERS
{
Newsletter nl = new Newsletter();
nl.FileName = Path.GetFileName(newsletter);
newsletters.Add(nl);
}
fldr.FolderName = thisFolder;
fldr.Newsletter = newsletters;
fldrs.Add(fldr);
}
return View(fldrs);
}
catch (Exception ex)
{
Errors.ErrorOccured(ex);
}
return RedirectToAction("Index", "Error", new { Area = "" });
}
But, I would recommend to use the LINQ methods OrderBy()
/OrderByDescending()
to sort the elements in ascending or descending order. Because of the original list of folders/files is sorted ascending by the Windows. But if this code will be used, for example, on the LINUX the result will be different.