# C#对象数组排序方法

#实现数组的排序

1using System;
2
3namespace DataStruct
4{
5 public class Sorter
6 {
7 /**//// <summary>
8 /// 冒泡排序法1
9 /// </summary>
10 /// <param name="list"></param>
11 public static void BubbleSort(int[] list)
12 {
13 for (int i = 0; i < list.Length; i++)
14 {
15 for (int j = i; j < list.Length; j++)
16 {
17 if (list[i] < list[j])
18 {
19 int temp = list[i];
20 list[i] = list[j];
21 list[j] = temp;
22 }
23 }
24 }
25 }
26
27 /**//// <summary>
28 /// 插入排序法
29 /// </summary>
30 /// <param name="list"></param>
31 public static void InsertionSort(int[] list)
32 {
33 for (int i = 1; i < list.Length; i++)
34 {
35 int t = list[i];
36 int j = i;
37 while ((j > 0) && (list[j - 1] > t))
38 {
39 list[j] = list[j - 1];
40 --j;
41 }
42 list[j] = t;
43 }
44
45 }
46
47 /**//// <summary>
48 /// 选择排序法
49 /// </summary>
50 /// <param name="list"></param>
51 public static void SelectionSort(int[] list)
52 {
53 int min;
54 for (int i = 0; i < list.Length - 1; i++)
55 {
56 min = i;
57 for (int j = i + 1; j < list.Length; j++)
58 {
59 if (list[j] < list[min])
60 min = j;
61 }
62 int t = list[min];
63 list[min] = list[i];
64 list[i] = t;
65 }
66
67 }
68
69 /**//// <summary>
70 /// 希尔排序法
71 /// </summary>
72 /// <param name="list"></param>
73 public static void ShellSort(int[] list)
74 {
75 int inc;
76 for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
77 for (; inc > 0; inc /= 3)
78 {
79 for (int i = inc + 1; i <= list.Length; i += inc)
80 {
81 int t = list[i - 1];
82 int j = i;
83 while ((j > inc) && (list[j - inc - 1] > t))
84 {
85 list[j - 1] = list[j - inc - 1];
86 j -= inc;
87 }
88 list[j - 1] = t;
89 }
90 }
91 }
92
93 private static void Swap(ref int l, ref int r)
94 {
95 int s;
96 s = l;
97 l = r;
98 r = s;
99 }
100
101 /**//// <summary>
102 /// 快速排序法
103 /// </summary>
104 /// <param name="list"></param>
105 /// <param name="low"></param>
106 /// <param name="high"></param>
107 public static void Sort(int[] list, int low, int high)
108 {
109 int pivot;
110 int l, r;
111 int mid;
112 if (high <= low)
113 return;
114 else if (high == low + 1)
115 {
116 if (list[low] > list[high])
117 Swap(ref list[low], ref list[high]);
118 return;
119 }
120 mid = (low + high) >> 1;
121 pivot = list[mid];
122 Swap(ref list[low], ref list[mid]);
123 l = low + 1;
124 r = high;
125 do
126 {
127 while (l <= r && list[l] < pivot)
128 l++;
129 while (list[r] >= pivot)
130 r--;
131 if (l < r)
132 Swap(ref list[l], ref list[r]);
133 } while (l < r);
134 list[low] = list[r];
135 list[r] = pivot;
136 if (low + 1 < r)
137 Sort(list, low, r - 1);
138 if (r + 1 < high)
139 Sort(list, r + 1, high);
140 }
141 }
142}
143

C#对象数组排序方法

private bool isReverse = false;
private void Sort(PersonalNotificationEntity [] list,string key)
{
if ( isReverse )
{
Array.Reverse(list);
isReverse = false;
}
else
{
int len = list.Length;
object [] keys = new object[len];
for(int i = 0 ; i < len ; i++)
{
keys[i] = type.InvokeMember(key,BindingFlags.GetField ,null,list[i],null);
}
Array.Sort(keys,list);
isReverse = true;
}
}

type.InvokeMember()方法可以得到对象实例的属性/字段值，这里使用的是字段