tags:

views:

539

answers:

1

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

I have some working code which allows me to group together lists of files by FileType.

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

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

What I would like to be able to do is order fileGroups by the FileType enumeration value and then each group within fileGroups by the FileDate.

+4  A: 
var sortedThing = fileGroups
                  .OrderBy(g => g.Key)             // (1) Order the groups
                  .Select(g => g.OrderBy(f => f.FileDate));   // (2) Order *each* group

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

Martinho Fernandes
It doesnt like g.Key in .OrderBy(g => g.Key), its complaining that my object doesn't contain a definition for Key?
Andrew
Sorry, my mistake, gotta reverse the order of the operations...
Martinho Fernandes
That did the trick, many thanks for your help.
Andrew