C#对象数组排序⽅法#实现数组的排序
typeof array
⼀个排序的类,⽤了⼏种⽅式实现的。
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)
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>
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;
Type type = typeof(PersonalNotificationEntity);
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;
}
}
这⾥使⽤了Array.Sort()和Array.Reverse()⽅法对数据进⾏正/反排序,变量isReverse做为反排序的标志位
⽅法传⼊了2个参数,⼀个是要排序的对象数组list,⼀个是排序关键字key,即要对象的根据哪个属性或字段来进⾏排序(这个值是等于对象的属性/字段名)
type.InvokeMember()⽅法可以得到对象实例的属性/字段值,这⾥使⽤的是字段
在得到数组中的每⼀个要排序的字段值后,把这个字段值数组做为Array.Sort()⽅法的参数传⼊,Sort⽅法就会将对象数按这个字段的值进⾏排序。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。