C # Grouping / sorting a shared list <> using LINQ

I need to group and sort the general list <>. I have a list of objects representing files, and each of these objects has the FileName, FileType, and FileDate property. FileType is defined as an enumeration.

I have working code that allows me to group file lists using FileType.

var fileGroups = fileList.GroupBy(f=> f.FileType)

foreach (var group in fileGroups )
{
   foreach (var file in group)
   {
   }
}

What I would like to do is to arrange the filegroups by the value of the FileType enumeration, and then to each group in the filegroups using FileDate.

+3
source share
1 answer
var sortedThing = fileGroups
                  .OrderBy(g => g.Key)             // (1) Order the groups
                  .Select(g => g.OrderBy(f => f.FileDate));   // (2) Order *each* group

You need groups sorted (1), and each of the groups sorted inside (2);

+5
source

Source: https://habr.com/ru/post/1722329/


All Articles