2016-06-30 180 views
-2

我有一個包含對象的列表的列表。排序對象列表的列表c#

List<List<Field>> records; 

Field對象包含ID和Value。

我需要使用Field記錄的屬性對頂級List進行排序。

排序需要說的是,對於每個記錄,使用ID選擇一個列表,然後通過值對父項進行排序。

所以,如果我有2個記錄,他們是這樣的:使用ID 1將選擇子列表中的對象,然後進行排序的父對象

List[0] -> List [ID=1 Value="Hello", ID=2 Value="World"] 
    List[1] -> List [ID=1 Value="It's", ID=2 Value="Me"] 

。例如,如果ID是2,那麼排序會替換0和1項,就像我在World之前所做的那樣。

有沒有簡單的方法來做到這一點?

謝謝。

+3

【如何排序列表列出了?](http://stackoverflow.com/questions/3104042/how-to-sort-a-list-of-lists) –

+0

可能重複的可能的複製http://stackoverflow.com/questions/925471/sorting-a-list-of-objects-in-c-sharp –

回答

1

以下是您正在尋找的示例:

using System; using System.Collections.Generic;使用System.Linq的 ;

public class Program 
{ 
    public static void Main() 
    { 
    var structure = new List<List<string>>(); 
    structure.Add(new List<string>() {"Hello", "World"}); 
    structure.Add(new List<string>() {"It's", "Me"}); 

    SortBySubIndex(structure, 0); 
    SortBySubIndex(structure, 1); 
    } 

    public static void SortBySubIndex(List<List<string>> obj, int index) 
    { 
    obj = obj.OrderBy(list => list[index]).ToList(); 
    Console.WriteLine("INDEX: " + index); 
    Console.WriteLine(obj[0][0]); 
    Console.WriteLine(obj[1][0]); 
    Console.WriteLine(); 
    } 
} 
+0

這很可愛。正是我在找什麼。 – Grey