/// <summary>
/// 课程类
/// </summary>
public class Course : IComparable<Course>
{
public Course() { }
public Course(int courseId, string courseName, int classHour, string teacher)
{
this.CourseId = courseId;
this.CourseName = courseName;
this.ClassHour = classHour;
this.Teacher = teacher;
}
public int CourseId { get; set; }//课程编号
public string CourseName { get; set; }//课程名称
public int ClassHour { get; set; }//课时
public string Teacher { get; set; }//主讲老师
//接口对应的比较方法(这个方法的签名,千万不要动)
public int CompareTo(Course other)
{
//return this.CourseId.CompareTo(other.CourseId);
return other.CourseId.CompareTo(CourseId);
//如果把this放到前面,表示按照升序排列,other放到前面就是按照降序排列
}
}
#region 自定义排序类:根据需要,添加对应个数的排序类
/// <summary>
/// 课程编号升序
/// </summary>
class CourseIdASC : IComparer<Course>
{
public int Compare(Course x, Course y)
{
return x.CourseId.CompareTo(y.CourseId);
}
}
/// <summary>
/// 课程编号降序
/// </summary>
class CourseIdDESC : IComparer<Course>
{
public int Compare(Course x, Course y)
{
return y.CourseId.CompareTo(x.CourseId);
}
}
/// <summary>
/// 课时升序
/// </summary>
class CourseClassASC : IComparer<Course>
{
public int Compare(Course x, Course y)
{
return x.ClassHour.CompareTo(y.ClassHour);
}
}
#endregion
Console.WriteLine("\r\n-----------按照编号将序---------\r\n");
courseList.Sort(new CourseIdDESC());