- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 集合类测试
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Student> stu = new List<Student>();
- Student[] st = { new Student("黑马—冯华亮", 111),new Student("黑马-孤独求败", 22), new Student("黑马-马克西姆", 34) };
- //Student[] st5 = { new Student(){stuname="zhangsan",stunum=27},new Student(){stuname="lisi",stunum=29}};
- for (int i = 0; i < st.Length; i++)
- {
- stu.Add(st[i]);
- }
- stu.Sort(Student.numobj);
- foreach(Student s in stu)
- {
- Console.WriteLine(s.ToString());
- Console.ReadLine();
- }
- }
- }
- }class Student:IComparable<Student>
- {
- private string stuname;
- private int stunum;
- private class numsorter : IComparer<Student>
- {
- public int Compare(Student stu1, Student stu2)
- {
- return stu1.stuname.CompareTo(stu2.stuname);
- }
- }
- public static IComparer<Student> numobj
- {
- get { return new numsorter(); }
- }
- public Student(string name, int num)
- {
- stuname = name;
- stunum = num;
- }
- //public string stuname { get; set; }
- //public int stunum { get; set; }
- public override string ToString()
- {
- return "该生的信息:\n" + stuname + "---" + stunum;
- }
- public int CompareTo(Student stu1)
- {
- return this.stunum.CompareTo(stu1.stunum);
- }
- }
复制代码 |