欢迎来到飞鸟慕鱼博客,开始您的技术之旅!
当前位置: 首页知识笔记正文

Title: Make the class support sorting

终极管理员 知识笔记 54阅读

1 use the system; 2 use the system. Assemble. Generic drugs; 3 use the system. Text; 4 use the system. Collection; 56 Namespace Console Application 17 {8/* *//Abstract gt; 9///Inherit the I-comparable interface, so that the class can support sorting. The sorting algorithm is in the sorting class 10///Summary 11 Public Class Student : I Comparable 12 {13 PrivateString m _ name; 14privateintm _ Score1516

, 0, 255, 1)">public Student() { }
 17
 18        public Student(string name, int score) 
 19        {
 20            this.m_Name = name;
 21            this.m_Score = score;
 22        }

 23
 24        public string Name
 25        {
 26            get return m_Name; }
 27            set { m_Name = value; }
 28        }

 29
 30        public int Score
 31        {
 32            get return m_Score; }
 33            set { m_Score = value; }
 34        }

 35
 36        IComparable 成员
 65      
 66    }
 67
 68    public class StudentSortByName :  IComparer 
 69    {
 70
 71        IComparer 成员
 91    }

 92
 93    class Program
 94    {
 95        static void Main(string[] args)
 96        {
 97            Student[] students = new Student[10];
 98            Random rd = new Random();
 99            for (int i = 0; i < 10; i++
100            {
101                students[i] = new Student("Student" + i.ToString(), rd.Next(100));
102            }

103            Console.WriteLine("分数从小到大排序");
104            Array.Sort(students);
105            ShowStudents(students);
106
107            Console.WriteLine("分数从大到小排序");
108            Array.Reverse(students);
109            ShowStudents(students);
110
111            Console.WriteLine("以学生名称排序");
112            Array.Sort(students, new StudentSortByName());
113            ShowStudents(students);
114            Console.ReadLine();
115
116
117        }

118
119        private static void ShowStudents(Student[] students)
120        {
121            foreach (Student s in students)
122            {
123                Console.WriteLine("Name :" + s.Name + " Score :" + s.Score.ToString());
124            }

125        }

126    }

127}
128

标签:
声明:无特别说明,转载请标明本文来源!