簡體   English   中英

如何在C#中對由列表組成的列表進行排序?

[英]How to sort List made of Lists in C#?

我有一堂課,看起來像這樣:

 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是原始類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和憑據未正確排序,因為它們的日期不是降序而是隨機的。

如何對第一項:內部列表中的項進行排序,第二種:大列表中的項以及所有基於日期的排序?

尚不清楚您想要什么,但我認為您可能可以使用以下內容:

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,2,5 {{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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM