如何在 C# 中对由列表组成的列表进行排序?

How to sort List made of Lists in C#?

我有一个 class 看起来像这样:

 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; }
    }

我制作了一个列表列表,如下所示:

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

之后我用一些列表填充我的 historyRecord

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

其中 bla bla 是原始 class HistoryViewModel 的属性。

我想按日期对我的列表列表进行排序 属性 但如果它们仅在日期上不同,它不会对列表列表中的项目进行排序。

这里是排序部分:

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

我得到的东西看起来像这样:

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     

因此,测试 2 和凭据没有正确排序,因为它们的日期不是降序而是随机的。

首先如何排序:内部列表中的项目和大列表中的 second:lists 以及所有基于日期的项目?

还不完全清楚你想要什么,但我想你可以使用这样的东西:

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

这应该会为您提供所有项目的单个列表,按日期排序。

编辑: 如果你想颠倒顺序...

.OrderByDescending(record => record.DateOfEvent);

不清楚您要实现的目标。让我们将问题简化为 List<List<int>>:

这个列表列表的输出应该是什么?:{{1, 2, 5}, {2, 1, 7}, {0, 5, 3}}

  1. 它应该是一个按元素排序的扁平列表吗?

    {0, 1, 1, 2, 2, 3, 5, 5, 7}

  2. 是否应该是按每个列表的元素排序的排序列表的列表?

    {{0, 3, 5}, {1, 2, 5}, {1, 2, 7}}

  3. 还有别的吗?

第一种情况很容易解决 SelectMany:

List<List<int>> listOfLists = 
                  new List<List<int>> { 
                        new List<int> { 2, 1, 7 }, 
                        new List<int> { 0, 5, 3 }, 
                        new List<int> { 1, 2, 5 } };

var sorted = listOfLists.SelectMany(l => l).OrderBy(l => l);
Console.WriteLine(string.Join(" ", sorted));

在第二种情况下,您需要先迭代以对所有列表进行排序:

List<List<int>> listOfLists = new List<List<int>> { 
                        new List<int> { 1, 2, 5 }, 
                        new List<int> { 2, 1, 7 }, 
                        new List<int> { 0, 5, 3 } };

foreach (var list in listOfLists)
{
    list.Sort();
}

var sorted = listOfLists.OrderBy(l => l.First());

foreach (var list in sorted)
{
    Console.WriteLine(string.Join(" ", list));
}