using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace 学生list集合
{
class Program
{
static void Main(string[] args)
{
List<Studetclass> classstudent = new List<Studetclass>();//定义一个班级泛型集合
Studetclass student1 = new Studetclass();
student1.Name = "A";
student1.sex = "男";
student1.Age = 18;
student1.ID = 1;
student1.Years = DateTime.Parse("2013-01-03");
student1.Score = new List<int> { 78, 89, 45, 78, 99, 66 };
Studetclass student2 = new Studetclass();
student2 .Name = "C";
student2 .sex = "女";
student2 .Age = 17;
student2 .ID = 2;
student2.Years = DateTime.Parse("2013-01-05");
student2 .Score = new List<int> { 78, 89, 45, 78, 99, 66 };
Studetclass student3 = new Studetclass();
student3.Name = "B";
student3.sex = "女";
student3.Age = 17;
student3.ID = 2;
student3.Years = DateTime.Parse("2013-01-02");
student3.Score = new List<int> { 78, 89, 45, 78, 99, 66 };
classstudent.Add(student1);
classstudent.Add(student2);
classstudent.Add(student3);
Console.WriteLine("------------------未排序前面--------------------------");
for (int i = 0; i < classstudent.Count; i++)
{
Console.WriteLine(classstudent[i].Name);
}
Console.WriteLine("------------------排序之后--------------------------");
//Comparison<Student> com = new Comparison<Student>(s.Comparison);//委托的方法
//Array.Sort(student, com);
//foreach (Student item in student)
//{
// Console.WriteLine(item.出生年月);
//}
//foreach (Student item in classstudent)
//{
// Console.WriteLine(item.姓名);
//}
//Array.Sort(student, new Student());
//IComparer ICom = new Student();
//通过Comparison<T> 委托实现按照出生年月日排序,输出排序后结果
//Compare cp = new Compare();
//classstudent.Sort(new Comparison<Studetclass>(cp.MyComparison));
//foreach (var item in classstudent)
//{
// Console.WriteLine(item.Years);
//}
//通过IComparer<T>接口实现按照姓名排序,输出排序后结果。
查找到班级中姓名然后来排序
IEnumerable<Studetclass> query = from items in classstudent orderby items.Name select items;
foreach (var item in query)//遍历输出排序后的结果
{
Console.WriteLine(item.Name);
}
//IEnumerable<Studetclass> query1 = from items in classstudent orderby items.Years select items;
//foreach (var item in query1)//遍历输出排序后的结果
//{
// Console.WriteLine(item.Years);
//}
//foreach (var student in classstudent)
//{
// Console.WriteLine(student.Name);
//}
Console.ReadKey();
}
}
//创建学生班级
public class Studetclass:IComparer
{
public string Name;
public int Age;
public string sex;
public int ID;
public List<int> Score;
public DateTime Years;
#region IComparer 成员
public int Compare(object x, object y)
{
Studetclass a = x as Studetclass;
Studetclass b = y as Studetclass;
return a.Name.CompareTo(b.Name);
}
#endregion
}
//public class Compare
//{
// public int MyComparison(Studetclass x, Studetclass y)
// {
// if (Convert.ToDateTime(x.Years) > Convert.ToDateTime(y.Years))
// {
// return 1;
// }
// if (Convert.ToDateTime(x.Years) == Convert.ToDateTime(y.Years))
// {
// return 0;
// }
// return -1;
// }
//}
}
学生list集合
最新推荐文章于 2024-02-22 14:55:54 发布