首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#中怎么对对象数组排序

2012-09-25 
C#中如何对对象数组排序从键盘接收4名学生的信息(学号、姓名、成绩)并按照成绩由高到低排列,用对象数组做。新

C#中如何对对象数组排序
从键盘接收4名学生的信息(学号、姓名、成绩)并按照成绩由高到低排列,用对象数组做。新手求指导

[解决办法]

C# code
class Student{    public int ID { get; set; }    public string Name { get; set; }    public int Score { get; set; }}void Main(){    List<Student> students = new List<Student>();    for (int i = 1; i <= 4; i++)    {        Console.WriteLine("Please input the student name:");        string name = Console.ReadLine();        Console.WriteLine("Please input {0}'s ID:", Name);        int id = int.Parse(Console.ReadLine());        Console.WriteLine("Please input {0}'s Score:", Name);        int score = int.Parse(Console.ReadLine());        students.Add(new Student() { Name = name, ID = id, Score = score });    }    foreach (Student student in students.OrderByDescending(x => x.Score))    {        Console.WriteLine(student.Name);    }} 

热点排行