对对象列表的列表进行排序c#

对对象列表的列表进行排序c#,c#,linq,list,sorting,C#,Linq,List,Sorting,我有一个包含一个对象的列表 List<List<Field>> records; 使用ID 1将选择子列表中的对象,然后对父对象进行排序。例如,如果ID为2,排序将交换0和1项,因为Me位于World之前 有没有一个简单的方法可以做到这一点 谢谢。以下是您正在寻找的示例: 使用制度; 使用System.Collections.Generic; 使用System.Linq public class Program { public static void Main()

我有一个包含一个对象的列表

List<List<Field>> records;
使用ID 1将选择子列表中的对象,然后对父对象进行排序。例如,如果ID为2,排序将交换0和1项,因为Me位于World之前

有没有一个简单的方法可以做到这一点


谢谢。

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

使用制度; 使用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();
  }
}
公共类程序
{
公共静态void Main()
{
var结构=新列表();
Add(newlist(){“Hello”,“World”});
Add(newlist(){“It's”,“Me”});
SortBySubIndex(结构,0);
SortBySubIndex(结构,1);
}
公共静态无效SortBySubIndex(列表对象,整数索引)
{
obj=obj.OrderBy(list=>list[index]).ToList();
控制台写入线(“索引:”+INDEX);
Console.WriteLine(obj[0][0]);
Console.WriteLine(obj[1][0]);
Console.WriteLine();
}
}

可能重复的,可能重复的那很可爱。正是我想要的。
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();
  }
}