Skip to content

Instantly share code, notes, and snippets.

@kw0006667
Created October 23, 2012 19:11
Show Gist options
  • Save kw0006667/3940942 to your computer and use it in GitHub Desktop.
Save kw0006667/3940942 to your computer and use it in GitHub Desktop.
將10個學生的成績由高到底排序
namespace ListConsoleApp
{
public class Student
{
public string Name;
public int Score;
public Student(string name, int score)
{
this.Name = name;
this.Score = score;
}
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>(){ new Student("Student A", 90),
new Student("Student B", 75),
new Student("Student C", 83),
new Student("Student D", 94),
new Student("Student E", 60),
new Student("Student F", 56),
new Student("Student G", 30),
new Student("Student I", 73),
new Student("Student J", 68),
new Student("Student K", 46)};
foreach (var stu in students)
Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
students.Sort((x, y) => { return -x.Score.CompareTo(y.Score); });
// ---- 排序後(分數由高到低)
Console.WriteLine("\n----------排序後(分數由高到低)-------\n");
foreach (var stu in students)
Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment