Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Collections;
6
7namespace Flight
8{
9 public class Program
10 {
11 static void Main(string[] args)
12 {
13 /**//*泛型集合排序*/
14
15 Flistlist f1 = new Flistlist();
16 f1.FMoney = 200;
17 f1.FName = "国际航空";
18 f1.FNumber = 333;
19
20 Flistlist f2 = new Flistlist();
21 f2.FMoney = 1000;
22 f2.FName = "东方航空";
23 f2.FNumber = 777;
24
25 Flistlist f3 = new Flistlist();
26 f3.FMoney = 900;
27 f3.FName = "海南航空";
28 f3.FNumber = 780;
29
30 Flistlist f4 = new Flistlist();
31 f4.FMoney = 500;
32 f4.FName = "东方航空";
33 f4.FNumber = 999;
34
35
36
37 List<Flistlist> Flist = new List<Flistlist>();
38 Flist.Add(f1);
39 Flist.Add(f2);
40 Flist.Add(f3);
41 Flist.Add(f4);
42
43
44 foreach (Flistlist item in Flist)
45 {
46 Console.WriteLine(item.FMoney);
47 }
48
49 Console.WriteLine("-------------------------------------");
50
51 Comparer c = new Comparer();
52 //排序
53 Flist.Sort(c);
54
55
56 foreach (Flistlist fl in Flist)
57 {
58 Console.WriteLine(fl.FMoney);
59 }
60
61
62
63 /**//* Sort 方法排序*/
64
65 Flist.Sort(
66 delegate(Flistlist x, Flistlist y)
67 {
68 if (x == null)
69 {
70 if (y == null)
71 {
72 return 0;
73 }
74 else
75 {
76 return -1;
77 }
78 }
79 else if (y == null)
80 {
81 return 1;
82 }
83 else
84 {
85 return x.FName.CompareTo(y.FName);
86 }
87 });
88
89
90
91
92 foreach (Flistlist fl in Flist)
93 {
94 Console.WriteLine(fl.FName);
95 }
96
97
98
99
100
101
102 }
103
104
105 protected class StaClass
106 {
107 public StaClass() { }
108 public static void AAA()
109 {
110 Console.WriteLine("String");
111 }
112 }
113
114
115
116
117 }
118 [Serializable]
119 public class Flistlist
120 {
121 public int FMoney { get; set; }
122 public string FName { get; set; }
123 public int FNumber { get; set; }
124
125
126 }
127
128
129
130 //实现IComparer接口
131 public class Comparer : IComparer<Flistlist>
132 {
133 public int Compare(Flistlist x, Flistlist y)
134 {
135 if (x == null)
136 {
137 if (y == null)
138 {
139 return 0;
140 }
141 else
142 {
143 return -1;
144 }
145 }
146 else if (y == null)
147 {
148 return 1;
149 }
150 else
151 {
152 return x.FMoney.CompareTo(y.FMoney);
153 }
154 }
155
156 }
157}
158
159
160/**//* 整型排序 */
161using System;
162using System.Collections.Generic;
163using System.Linq;
164using System.Text;
165
166namespace Array
167{
168 class Program
169 {
170
171 private static int Compare(int i1, int i2)
172 {
173 if (i1 > i2) return 1;
174 if (i1 < i2) return -1;
175 return 0;
176 }
177
178 public static List<int> list;
179
180
181 static void Main(string[] args)
182 {
183 int[] a = new int[] { 3, 5, 6, 4, 2 };
184
185 list = new List<int>();
186
187 foreach (int i in a)
188 {
189 list.Add(i);
190 }
191
192 list.Sort(Compare);
193
194 foreach (int i in list)
195 {
196 Console.WriteLine(i);
197 }
198
199 Console.ReadLine();
200
201 List<int> l = new List<int>();
202
203 int[] arry = new int[] { 100,89,30,20,1000 };
204
205 foreach (int number in arry)
206 {
207 l.Add(number);
208 }
209
210
211 l.Sort(Compare);
212
213 foreach (int item in l)
214 {
215 Console.WriteLine(item);
216 }
217
218
219 Console.ReadLine();
220
221
222
223
224
225
226
227 }
228 }
229}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Collections;
6
7namespace Flight
8{
9 public class Program
10 {
11 static void Main(string[] args)
12 {
13 /**//*泛型集合排序*/
14
15 Flistlist f1 = new Flistlist();
16 f1.FMoney = 200;
17 f1.FName = "国际航空";
18 f1.FNumber = 333;
19
20 Flistlist f2 = new Flistlist();
21 f2.FMoney = 1000;
22 f2.FName = "东方航空";
23 f2.FNumber = 777;
24
25 Flistlist f3 = new Flistlist();
26 f3.FMoney = 900;
27 f3.FName = "海南航空";
28 f3.FNumber = 780;
29
30 Flistlist f4 = new Flistlist();
31 f4.FMoney = 500;
32 f4.FName = "东方航空";
33 f4.FNumber = 999;
34
35
36
37 List<Flistlist> Flist = new List<Flistlist>();
38 Flist.Add(f1);
39 Flist.Add(f2);
40 Flist.Add(f3);
41 Flist.Add(f4);
42
43
44 foreach (Flistlist item in Flist)
45 {
46 Console.WriteLine(item.FMoney);
47 }
48
49 Console.WriteLine("-------------------------------------");
50
51 Comparer c = new Comparer();
52 //排序
53 Flist.Sort(c);
54
55
56 foreach (Flistlist fl in Flist)
57 {
58 Console.WriteLine(fl.FMoney);
59 }
60
61
62
63 /**//* Sort 方法排序*/
64
65 Flist.Sort(
66 delegate(Flistlist x, Flistlist y)
67 {
68 if (x == null)
69 {
70 if (y == null)
71 {
72 return 0;
73 }
74 else
75 {
76 return -1;
77 }
78 }
79 else if (y == null)
80 {
81 return 1;
82 }
83 else
84 {
85 return x.FName.CompareTo(y.FName);
86 }
87 });
88
89
90
91
92 foreach (Flistlist fl in Flist)
93 {
94 Console.WriteLine(fl.FName);
95 }
96
97
98
99
100
101
102 }
103
104
105 protected class StaClass
106 {
107 public StaClass() { }
108 public static void AAA()
109 {
110 Console.WriteLine("String");
111 }
112 }
113
114
115
116
117 }
118 [Serializable]
119 public class Flistlist
120 {
121 public int FMoney { get; set; }
122 public string FName { get; set; }
123 public int FNumber { get; set; }
124
125
126 }
127
128
129
130 //实现IComparer接口
131 public class Comparer : IComparer<Flistlist>
132 {
133 public int Compare(Flistlist x, Flistlist y)
134 {
135 if (x == null)
136 {
137 if (y == null)
138 {
139 return 0;
140 }
141 else
142 {
143 return -1;
144 }
145 }
146 else if (y == null)
147 {
148 return 1;
149 }
150 else
151 {
152 return x.FMoney.CompareTo(y.FMoney);
153 }
154 }
155
156 }
157}
158
159
160/**//* 整型排序 */
161using System;
162using System.Collections.Generic;
163using System.Linq;
164using System.Text;
165
166namespace Array
167{
168 class Program
169 {
170
171 private static int Compare(int i1, int i2)
172 {
173 if (i1 > i2) return 1;
174 if (i1 < i2) return -1;
175 return 0;
176 }
177
178 public static List<int> list;
179
180
181 static void Main(string[] args)
182 {
183 int[] a = new int[] { 3, 5, 6, 4, 2 };
184
185 list = new List<int>();
186
187 foreach (int i in a)
188 {
189 list.Add(i);
190 }
191
192 list.Sort(Compare);
193
194 foreach (int i in list)
195 {
196 Console.WriteLine(i);
197 }
198
199 Console.ReadLine();
200
201 List<int> l = new List<int>();
202
203 int[] arry = new int[] { 100,89,30,20,1000 };
204
205 foreach (int number in arry)
206 {
207 l.Add(number);
208 }
209
210
211 l.Sort(Compare);
212
213 foreach (int item in l)
214 {
215 Console.WriteLine(item);
216 }
217
218
219 Console.ReadLine();
220
221
222
223
224
225
226
227 }
228 }
229}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253