日期:2014-05-17  浏览次数:20880 次

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