读书人

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

发布时间: 2012-09-25 09:55:59 作者: rapoo

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);    }} 

读书人网 >C#

热点推荐