我有 3 个列表,其中包含:索引、姓名、年龄
示例:
List<int> indexList = new List<int>();
indexList.Add(3);
indexList.Add(1);
indexList.Add(2);
List<string> nameList = new List<string>();
nameList.Add("John");
nameList.Add("Mary");
nameList.Add("Jane");
List<int> ageList = new List<int>();
ageList.Add(16);
ageList.Add(17);
ageList.Add(18);
我现在必须根据 indexList 对所有 3 个列表进行排序。
如何在对其他 2 个列表进行排序时对 indexList 使用 .sort()
最佳答案
你看错了。创建自定义类:
class Person
{
public int Index { get; set; }
public string Name{ get; set; }
public int Age{ get; set; }
}
然后,对 List<Person>
进行排序在 OrderBy
的帮助下来自 System.Linq
的方法命名空间:
List<Person> myList = new List<Person>() {
new Person { Index = 1, Name = "John", Age = 16 };
new Person { Index = 2, Name = "James", Age = 19 };
}
...
var ordered = myList.OrderBy(x => x.Index);
此外,您还可以阅读 Jon Skeet article关于你的反模式。
关于c# - 排序多个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30033904/