C#泛型集合排序

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Collections;
  6
  7namespace Flight
  8ExpandedBlockStart.gifContractedBlock.gif{
  9    public class Program
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 11        static void Main(string[] args)
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 13ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*泛型集合排序*/
 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)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 58                Console.WriteLine(fl.FMoney);
 59            }

 60
 61
 62
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//* Sort 方法排序*/
 64
 65            Flist.Sort(
 66               delegate(Flistlist x, Flistlist y)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif               {
 68                   if (x == null)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif                   {
 70                       if (y == null)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                       {
 72                           return 0;
 73                       }

 74                       else
 75ExpandedSubBlockStart.gifContractedSubBlock.gif                       {
 76                           return -1;
 77                       }

 78                   }

 79                   else if (y == null)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                   {
 81                       return 1;
 82                   }

 83                   else
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                   {
 85                       return x.FName.CompareTo(y.FName);
 86                   }

 87               }
);
 88
 89
 90
 91
 92            foreach (Flistlist fl in Flist)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 94                Console.WriteLine(fl.FName);
 95            }

 96
 97
 98
 99
100
101
102        }

103
104
105        protected class StaClass
106ExpandedSubBlockStart.gifContractedSubBlock.gif        {
107ExpandedSubBlockStart.gifContractedSubBlock.gif            public StaClass() { }
108            public static void AAA()
109ExpandedSubBlockStart.gifContractedSubBlock.gif            {
110                Console.WriteLine("String");
111            }

112        }

113
114
115
116
117    }

118    [Serializable]
119    public class Flistlist
120ExpandedSubBlockStart.gifContractedSubBlock.gif    {
121ExpandedSubBlockStart.gifContractedSubBlock.gif        public int FMoney getset; }
122ExpandedSubBlockStart.gifContractedSubBlock.gif        public string FName getset; }
123ExpandedSubBlockStart.gifContractedSubBlock.gif        public int FNumber getset; }
124
125
126    }

127
128
129
130    //实现IComparer接口 
131    public class Comparer : IComparer<Flistlist>
132ExpandedSubBlockStart.gifContractedSubBlock.gif    {
133        public int Compare(Flistlist x, Flistlist y)
134ExpandedSubBlockStart.gifContractedSubBlock.gif        {
135            if (x == null)
136ExpandedSubBlockStart.gifContractedSubBlock.gif            {
137                if (y == null)
138ExpandedSubBlockStart.gifContractedSubBlock.gif                {
139                    return 0;
140                }

141                else
142ExpandedSubBlockStart.gifContractedSubBlock.gif                {
143                    return -1;
144                }

145            }

146            else if (y == null)
147ExpandedSubBlockStart.gifContractedSubBlock.gif            {
148                return 1;
149            }

150            else
151ExpandedSubBlockStart.gifContractedSubBlock.gif            {
152                return x.FMoney.CompareTo(y.FMoney);
153            }

154        }

155
156    }

157}

158
159
160ExpandedBlockStart.gifContractedBlock.gif/**//* 整型排序 */
161using System;
162using System.Collections.Generic;
163using System.Linq;
164using System.Text;
165
166namespace Array
167ExpandedBlockStart.gifContractedBlock.gif{
168    class Program
169ExpandedSubBlockStart.gifContractedSubBlock.gif    {
170
171        private static int Compare(int i1, int i2)
172ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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)
182ExpandedSubBlockStart.gifContractedSubBlock.gif        {
183ExpandedSubBlockStart.gifContractedSubBlock.gif            int[] a = new int[] 35642 };
184
185            list = new List<int>();
186
187            foreach (int i in a)
188ExpandedSubBlockStart.gifContractedSubBlock.gif            {
189                list.Add(i);
190            }

191
192            list.Sort(Compare);
193
194            foreach (int i in list)
195ExpandedSubBlockStart.gifContractedSubBlock.gif            {
196                Console.WriteLine(i);
197            }

198
199            Console.ReadLine();
200
201            List<int> l = new List<int>();
202
203ExpandedSubBlockStart.gifContractedSubBlock.gif            int[] arry = new int[] 100,89,30,20,1000 };
204
205            foreach (int number in arry)
206ExpandedSubBlockStart.gifContractedSubBlock.gif            {
207                l.Add(number);
208            }

209
210
211            l.Sort(Compare);
212
213            foreach (int item in l)
214ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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

转载于:https://www.cnblogs.com/liuyong/archive/2009/06/01/1494079.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值