1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Collections;
5![](/Images/OutliningIndicators/None.gif)
6
namespace
ConsoleApplication1
7![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
9
/// 继承IComparable接口,使类可以支持排序,排序的算法在Array类中
10
/// </summary>
11
public class Student : IComparable
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
private string m_Name;
14
private int m_Score;
15![](/Images/OutliningIndicators/InBlock.gif)
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Student()
{ }
17![](/Images/OutliningIndicators/InBlock.gif)
18
public Student(string name, int score)
19![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
this.m_Name = name;
21
this.m_Score = score;
22
}
23![](/Images/OutliningIndicators/InBlock.gif)
24
public string Name
25![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_Name; }
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_Name = value; }
28
}
29![](/Images/OutliningIndicators/InBlock.gif)
30
public int Score
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_Score; }
33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_Score = value; }
34
}
35![](/Images/OutliningIndicators/InBlock.gif)
36![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IComparable 成员#region IComparable 成员
37![](/Images/OutliningIndicators/InBlock.gif)
38
//比较分数
39
public int CompareTo(object obj)
40![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
if (obj.GetType() != this.GetType())
42![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
throw new ArgumentException();
44
}
45
if (this.Equals(obj))
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
return 0;
48
}
49
Student s = (Student)obj;
50
if (s.Score == this.Score)
51![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
return 0;
53
}
54
else if (this.Score > s.Score)
55![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
return 1;
57
}
58
else
59![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
return -1;
61
}
62
}
63![](/Images/OutliningIndicators/InBlock.gif)
64
#endregion
65
66
}
67![](/Images/OutliningIndicators/InBlock.gif)
68
public class StudentSortByName : IComparer
69![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70![](/Images/OutliningIndicators/InBlock.gif)
71![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IComparer 成员#region IComparer 成员
72![](/Images/OutliningIndicators/InBlock.gif)
73
//以学生名称排序
74
public int Compare(object x, object y)
75![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
if (!(x is Student) || !(y is Student))
77![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
throw new ArgumentException();
79
}
80
if (x.Equals(y))
81![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
return 0;
83
}
84
Student s1 = (Student)x;
85
Student s2 = (Student)y;
86![](/Images/OutliningIndicators/InBlock.gif)
87
return string.Compare(s1.Name, s2.Name);
88
}
89![](/Images/OutliningIndicators/InBlock.gif)
90
#endregion
91
}
92![](/Images/OutliningIndicators/InBlock.gif)
93
class Program
94![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
static void Main(string[] args)
96![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
Student[] students = new Student[10];
98
Random rd = new Random();
99
for (int i = 0; i < 10; i++)
100![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
students[i] = new Student("Student" + i.ToString(), rd.Next(100));
102
}
103
Console.WriteLine("分数从小到大排序");
104
Array.Sort(students);
105
ShowStudents(students);
106![](/Images/OutliningIndicators/InBlock.gif)
107
Console.WriteLine("分数从大到小排序");
108
Array.Reverse(students);
109
ShowStudents(students);
110![](/Images/OutliningIndicators/InBlock.gif)
111
Console.WriteLine("以学生名称排序");
112
Array.Sort(students, new StudentSortByName());
113
ShowStudents(students);
114
Console.ReadLine();
115![](/Images/OutliningIndicators/InBlock.gif)
116![](/Images/OutliningIndicators/InBlock.gif)
117
}
118![](/Images/OutliningIndicators/InBlock.gif)
119
private static void ShowStudents(Student[] students)
120![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
foreach (Student s in students)
122![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
Console.WriteLine("Name :" + s.Name + " Score :" + s.Score.ToString());
124
}
125
}
126
}
127
}
128
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
125
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
128
![](/Images/OutliningIndicators/None.gif)