此例子采用委托来排序,整体思路如下:
建一Sorter类,用意是对所有的对象数组排序,不论这个对象是什么类型,只要它满足了Sorter类中的带的一个委托即可,然后新建一个员工类,用这个Sorter类来对一堆员工的工资进行排序.
好了,第一步,新建Sorter类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace BubbleSorter2
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
class Sorter
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
// 参数内的对象比较的方法
10
public delegate bool IsGreater(object a,object b);
11
// 采用冒泡比较的方法对对象数组内的对象进行比较
12
public static void Sort(object[] objs, IsGreater Compare)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
for (int i = 0; i < objs.Length-1; i++)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
bool flag = false;
17
object temp;
18
if (!flag)
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
for (int j = objs.Length - 1; j > i; j--)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
if (!Compare(objs[j], objs[j - 1]))
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
temp = objs[j - 1];
25
objs[j - 1] = objs[j];
26
objs[j] = temp;
27
}
28
flag = true;
29
}
30
}
31
}
32
}
33
}
34
}
第二步,新建员工类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace BubbleSorter2
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
class Employee
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
private string name;
10
private decimal salary;
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
public Employee(string par_name,decimal par_salary)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
name = par_name;
15
salary = par_salary;
16
}
17
public string Name
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
get
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
return this.name;
22
}
23
set
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
name = value;
26
}
27
}
28
public decimal Salary
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
get
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return salary;
33
}
34
set
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
salary = value;
37
}
38
}
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
// 这里满足了Sorter类要求的排序委托,也就是提供一个比较两个员工工资的方法
41
public static bool IsGeater(object a, object b)
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
if (((Employee)a).salary >= ((Employee)b).salary)
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
return true;
46
}
47
else
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
return false;
50
}
51
}
52
}
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后在主函数内进行调用
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
namespace BubbleSorter2
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8
class Program
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
static void Main(string[] args)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
// 这里用代码新建几个员工
13
Employee[] employees =
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
new Employee("Mr a",40000),
16
new Employee("Miss b",5000),
17
new Employee("Mrs c",10000),
18
new Employee("BOSS d",4000000),
19
new Employee("Sir e",300000),
20
new Employee("boy f",444444)
21
};
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
PrintValues(employees);
24
Sorter.Sort(employees, Employee.IsGeater);
25
PrintValues(employees);
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
Console.ReadKey();
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
public static void PrintValues(IEnumerable myList)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
foreach (object obj in myList)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
Console.WriteLine("{0}", ((Employee)obj).Salary);
35
}
36
Console.WriteLine();
37
}
38
}
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
大功告成.