c# - 排序多个列表

标签 c# sorting

我有 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/

相关文章:

c# - 如何将动态复选框设置为真/假

c# - 将数字 11 格式化为 00000011

java - (java)为什么冒泡排序不排序?

C++ 排序数组递归

c# - 如何使 WHERE 子句中的项目可选?

c# - .NET 中的动态类初始化

c# - 在 javascript 中使用 Url.Action

python - 根据元组列表对元组列表进行排序 - Python

python-3.x - 按名称对字典列表进行排序并分配新键

JavaScript 对象按日期排序