[size=16px]当泛型类list<T>内T为类是,类中又有多个字段,这是怎么按类中某个字段对list内类对象排序??[/size]
直接看代码: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;
}
}
}
直接看代码: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;
}
}
}
如果需要先按某排序,再按某某排序,还可以这样
list = list.OrderBy(x => x.某).ThenBy(x => x.某某).ToList();
tList.Sort((a, b) => { return a.SAge.CompareTo(b.SAge); });
foreach(T t in result)
{
//console
}
ForEach(t => Console.WriteLine());
效果一样
二楼三楼的写的更是全,这是没有道理的。
事实上,linq表达式和linq操作符是等价的,除非你愿意多打字。
//默认比较器进行比较,具体该怎么实现呢?研究好几天了,特来请教高手帮忙!System.Comparison是个委托
tList.Sort((a, b) => { return a.SAge.CompareTo(b.SAge); });
这种调用方式就是传入的就是一个Comparison委托,
其中a.SAge.CompareTo(b.SAge)的具体比较方式可以自己定义
list<T>的sort方法也可以接受一个实现了IComparer接口比较器对象。
比如: 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);linq或lambda表达式只是一种书写形式,最终比较还得看比较器的定义。