110,561
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleQuickSort
{
class Program
{
static void Main(string[] args)
{
List<Student> Lstudent = new List<Student>();
Lstudent.Add(new Student("周杰伦", '男', "1115186011", "机械制造", 20));
Lstudent.Add(new Student("赵飞燕", '女', "1115186012", "网络技术", 19));
Lstudent.Add(new Student("李煜", '男', "1115186013", "软件工程", 21));
Lstudent.Add(new Student("刘丹", '女', "1115186014", "临床护理", 20));
Lstudent.Add(new Student("李元芳", '男', "1115186015", "会计", 22));
Lstudent.Add(new Student("郭敬明", '男', "1115186016", "计算机科学", 23));
Lstudent.Add(new Student("刘艳艳", '女', "1115186017", "国际医学", 20));
Lstudent.Add(new Student("李晓霞", '女', "1115186018", "临床护理", 22));
Lstudent.Add(new Student("林丹", '男', "1115186019", "机械制造", 21));
Lstudent.Add(new Student("陈一冰", '男', "1115186020", "软件工程", 22));
//按年龄升序排列,然后输出所有同学所有信息,该用什么方法?
//list<T>泛型类自带有sort()排序,有几个重载,要使用指定的System.Comparison比较器,或
//默认比较器进行比较,具体该怎么实现呢?研究好几天了,特来请教高手帮忙!
}
}
//学生信息类
public class Student
{
//姓名
private string sName;
public string SName
{
get { return sName; }
set { sName = value; }
}
//性别
private char sGender;
public char SGender
{
get { return sGender; }
set { sGender = value; }
}
//学号
private string sCno;
public string SCno
{
get { return sCno; }
set { sCno = value; }
}
//专业
private string sMajor;
public string SMajor
{
get { return sMajor; }
set { sMajor = value; }
}
//年龄
private int sAge;
public int SAge
{
get { return sAge; }
set { sAge = value; }
}
//构造函数
public Student(string sName, char sGender, string sCno, string sMajor, int sAge)
{
this.SName = sName;
this.SGender = sGender;
this.SCno = sCno;
this.SMajor = sMajor;
this.SAge = sAge;
}
}
}
public class MyStudentComparer: IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.SAge.CompareTo(y.SAge); //这里可以使用自己定义的比较方式
}
}
//调用
MyStudentComparer sComparer = new MyStudentComparer();
tList.Sort(sComparer);
Lstudent.OrderBy(t => t.SAge).ToList().ForEach(t => Console.WriteLine(t.SName + ":" + t.SAge));