日期:2014-05-19  浏览次数:21053 次

一个排序,用代码实现
学生,成绩做倒序排,成绩相同,按年龄小的排前,年龄也同,按姓名作排序,不用数据库,直接用代码来实现  


------解决方案--------------------
姓名?成绩?年龄?神仙?妖怪...
是怎么保存的?
文本文件?结构数组?

你是不是看到很多问号?
------解决方案--------------------
按成绩降序,年龄升序,姓名升序??

for exampe;

class Program
{
static void Main(string[] args)
{
List <Student> array = new List <Student> ();
Student st = new Student( "aaa ", 23, 88);
array.Add(st);
st = new Student( "bbb ", 24, 90);
array.Add(st);
st = new Student( "fff ", 21, 88);
array.Add(st);
st = new Student( "ddd ", 21, 88);
array.Add(st);
Console.WriteLine( "Before Sort: ");
foreach (Student s in array)
{
Console.WriteLine(s.sname + "\t " + s.sage + "\t " + s.score);
}
array.Sort();
Console.WriteLine( "After Sort: ");
foreach (Student s in array)
{
Console.WriteLine(s.sname + "\t " + s.sage + "\t " + s.score);
}


}
}
struct Student : IComparable
{
public string sname;
public int sage;
public int score;
public Student(string sname, int sage, int score)
{
this.sname = sname;
this.sage = sage;
this.score = score;
}
public int CompareTo(object obj)
{
Student st = (Student)obj;
if (this.score != st.score)
return st.score.CompareTo(this.score);
if (this.sage != st.sage)
return this.sage.CompareTo(st.sage);
if (this.sname != st.sname)
return this.sname.CompareTo(st.sname);
return 1;
}
}

Before Sort:
aaa 23 88
bbb 24 90
fff 21 88
ddd 21 88
After Sort:
bbb 24 90
ddd 21 88
fff 21 88
aaa 23 88

------解决方案--------------------
楼上正解