Search code examples
c#.netsortingin-place

How to sort List made of Lists in C#?


I have a class that looks like this:

 public class HistoryViewModel
    {
        [Display(Name="Name")]
        public string ItemName { get; set; }

        [Display(Name="Original Path")]
        public string ItemPath { get; set; }

        [Display(Name="Type")]
        public string ItemType { get; set; }

        [Display(Name="Author")]
        public string EventAuthorName { get; set; }

        [Display(Name="Comment")]
        public string EventActionDesc { get; set; }

        [Display(Name="Timestamp")]
        public DateTime DateOfEvent { get; set; }

        [Display(Name="Action Type")]
        public string ActionType { get; set; }
    }

I have made a list of lists that looks like this:

List<List<HistoryViewModel>> historyRecord = new List<List<HistoryViewModel>>();

After that I fill my historyRecord with some lists by:

historyRecord.Add(new List<HistoryViewModel>{bla bla});

where bla bla are properties of original class HistoryViewModel.

I would like to sort my list of lists by date property but it does not sort my items inside list of lists if they differ only in date.

Here is sorting part:

historyRecord.Sort((x,y) => -1 * x[0].DateOfEvent.CompareTo(y[0].DateOfEvent));

I get sth that looks like this:

Type    Name    Path    Action  Date    Author  Comment
FOLDER  Test    C:\Workspace\Posao\BSM\Storage\LLLL\TTT     CREATE  21.1.2015. 10:24:05     
FILE    76.png  C:\Workspace\Posao\BSM\Storage\abcd\ZZZ     CREATE  21.1.2015. 10:18:58         
FOLDER  ZZZ     C:\Workspace\Posao\BSM\Storage\abcd     CREATE  21.1.2015. 10:07:42     
FOLDER  Test 2  C:\Workspace\Posao\BSM\Storage\abcd     CREATE  21.1.2015. 9:41:56  
FOLDER  Test 2  C:\Workspace\Posao\BSM\Storage\abcd     DELETE  21.1.2015. 9:42:12  
FILE    ZahtjeviZaPromjenu.xlsx     C:\Workspace\Posao\BSM\Storage\abcd\Testni  CREATE  21.1.2015. 8:43:10      
FILE    ZahtjeviZaPromjenu.xlsx     C:\Workspace\Posao\BSM\Storage\abcd\Testni  DELETE  21.1.2015. 9:06:45  
FILE    credentials.txt     C:\Workspace\Posao\BSM\Storage\abcd\Testni  CREATE  21.1.2015. 8:40:58      Uplodao Credentials za test
FILE    credentials.txt     C:\Workspace\Posao\BSM\Storage\abcd\Testni  DELETE  21.1.2015. 8:50:01      Deletano
FILE    credentials.txt     C:\Workspace\Posao\BSM\Storage\abcd\Testni  CREATE  21.1.2015. 9:10:14  
FILE    credentials.txt     C:\Workspace\Posao\BSM\Storage\abcd\Testni  DELETE  21.1.2015. 9:13:28      
FILE    credentials.txt     C:\Workspace\Posao\BSM\Storage\abcd\Testni  CREATE  21.1.2015. 10:07:50     

So, Test 2 and credentials are not sorted correctly as the dates for them don't go descending but random.

How do I sort first: items inside the inner lists and second:lists inside the big list and all that based on date?


Solution

  • It's not entirely clear what you want, but I think you might be able to use something like this:

    var sortedRecords = historyRecord
                           .SelectMany(record => record)
                           .OrderBy(record => record.DateOfEvent);
    

    That should give you a single list of all items, ordered by the date.

    Edit: And if you want the order reversed...

    .OrderByDescending(record => record.DateOfEvent);