Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对列表进行排序<;元组<;类别1、类别2>;?_C# - Fatal编程技术网

C# 如何对列表进行排序<;元组<;类别1、类别2>;?

C# 如何对列表进行排序<;元组<;类别1、类别2>;?,c#,C#,基本上,我想将包含不同类的元组添加到列表中,然后根据类2对对象进行排序 var myList = new List<Tuple<class1, class2>>(); myList.Add(new Tuple<class1, class2>(class1obj,class2obj)); var myList=new List(); Add(新元组(ClassObj,class2obj)); 如何根据类2中的属性对myList值进行排序???尝试以下操作:

基本上,我想将包含不同类的元组添加到列表中,然后根据类2对对象进行排序

var myList = new List<Tuple<class1, class2>>();
 myList.Add(new Tuple<class1, class2>(class1obj,class2obj));
var myList=new List();
Add(新元组(ClassObj,class2obj));
如何根据类2中的属性对myList值进行排序???

尝试以下操作:

var sortedList = myList.OrderBy(x => x.Item2.Property).ToList();
其中
Property
是要按其排序的属性

myList = myList.OrderBy(n => n.Item2.YourProperty).ToList();
我会给你结果的


(需要
使用System.Linq;

如果您有现有列表并希望对其进行排序,请调用指定要使用的方法。然后以任何适当的方式比较第二项

e、 g

var items = new List<Tuple<int, string>>
{
    Tuple.Create(1, "foo"),
    Tuple.Create(15, "bar"),
};
items.Sort((a, b) => a.Item2.CompareTo(b.Item2));
var items=新列表
{
Tuple.Create(1,“foo”),
Tuple.Create(15,“bar”),
};
items.Sort((a,b)=>a.Item2.CompareTo(b.Item2));

当然,也可以使用其他重载。这不是你唯一的选择。