标题:使用ArrayList和一个MyHash实现的排序
取消只看楼主
风情
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2005-3-31
 问题点数:0 回复次数:0 
使用ArrayList和一个MyHash实现的排序

using System; using System.Collections;

class Test {

//排序 public static void Main(string[] args) { ArrayList al = new ArrayList(); al.Add(new MyHash(10, 33)); al.Add(new MyHash(9, "afc")); al.Add(new MyHash(12, "Azf")); al.Add(new MyHash(11, "asfd")); al.Add(new MyHash(1, "jsdf")); al.Add(new MyHash(8, "nnbk")); al.Add(new MyHash(7, "cde")); al.Add(new MyHash(14, "Bxy"));

try { MyHash.type = "double"; MyHash.sort = "key"; al.Sort(new MySort(1,1)); } catch(Exception e ) { Console.WriteLine(e.ToString()); Console.WriteLine("按回车键结束"); Console.ReadLine(); return ; }

//打印数据 IEnumerator iem = al.GetEnumerator(); while (iem.MoveNext()) { Console.WriteLine(iem.Current); }

Console.WriteLine("按回车键结束"); Console.ReadLine(); }

}

class MyHash : IComparable {

//相当于Hashtable中的key和value public object key; private object val;

//数据类型,支持“char”、“sbyte”、“byte”、“int”、“short”、“long”、“float”、“double”、“string”、“ushort”、“uint”、“ulong” public static string type = "int";

//按照什么来排序,此处只能是“key”或“val” public static string sort = "key";

//构造器 public MyHash(object key, object val) { this.key = key; this.val = val; }

//重载ICompareable中的“CompareTo()”方法用来排序 public int CompareTo(object o) { if (o is MyHash) { MyHash tmp = (MyHash)o;

//按照key进行排序 if (MyHash.sort == "key") { switch(MyHash.type.ToLower()) { case "string": return this.key.ToString().CompareTo(tmp.key.ToString()); case "double": return double.Parse(this.key.ToString()).CompareTo(double.Parse(tmp.key.ToString())); case "float": return float.Parse(this.key.ToString()).CompareTo(float.Parse(tmp.key.ToString())); case "long": return long.Parse(this.key.ToString()).CompareTo(long.Parse(tmp.key.ToString())); case "int": return int.Parse(this.key.ToString()).CompareTo(int.Parse(tmp.key.ToString())); case "char": return char.Parse(this.key.ToString()).CompareTo(char.Parse(tmp.key.ToString())); case "short": return short.Parse(this.key.ToString()).CompareTo(short.Parse(tmp.key.ToString())); case "byte": return byte.Parse(this.key.ToString()).CompareTo(byte.Parse(tmp.key.ToString())); case "sbyte": return sbyte.Parse(this.key.ToString()).CompareTo(sbyte.Parse(tmp.key.ToString())); case "ushort": return ushort.Parse(this.key.ToString()).CompareTo(ushort.Parse(tmp.key.ToString())); case "uint": return uint.Parse(this.key.ToString()).CompareTo(uint.Parse(tmp.key.ToString())); case "ulong": return ulong.Parse(this.key.ToString()).CompareTo(ulong.Parse(tmp.key.ToString())); default : throw new ArgumentException("错误的数据类型"); }

//按照val进行排序 } else if (MyHash.sort == "val") { switch(MyHash.type.ToLower()) { case "string": return this.val.ToString().CompareTo(tmp.val.ToString()); case "double": return double.Parse(this.val.ToString()).CompareTo(double.Parse(tmp.val.ToString())); case "float": return float.Parse(this.val.ToString()).CompareTo(float.Parse(tmp.val.ToString())); case "long": return long.Parse(this.val.ToString()).CompareTo(long.Parse(tmp.val.ToString())); case "int": return int.Parse(this.val.ToString()).CompareTo(int.Parse(tmp.val.ToString())); case "char": return char.Parse(this.val.ToString()).CompareTo(char.Parse(tmp.val.ToString())); case "short": return short.Parse(this.val.ToString()).CompareTo(short.Parse(tmp.val.ToString())); case "byte": return byte.Parse(this.val.ToString()).CompareTo(byte.Parse(tmp.val.ToString())); case "sbyte": return sbyte.Parse(this.val.ToString()).CompareTo(sbyte.Parse(tmp.val.ToString())); case "ushort": return ushort.Parse(this.val.ToString()).CompareTo(ushort.Parse(tmp.val.ToString())); case "uint": return uint.Parse(this.val.ToString()).CompareTo(uint.Parse(tmp.val.ToString())); case "ulong": return ulong.Parse(this.val.ToString()).CompareTo(ulong.Parse(tmp.val.ToString())); default : throw new ArgumentException("错误的数据类型"); } } else { throw new ArgumentException("仅支持key或val的排序"); } } throw new ArgumentException("参数无效"); }

//此处重载了object中的“ToString()”方法,输出的是最终结果,可以根据需要自行修改 public override string ToString() { return "Key: " + this.key.ToString() + "\tValue: " + this.val.ToString(); }

}

//实现IComparer中的“Compare”方法用来排序 class MySort : IComparer { //0为降序,1为升序 private static int n = 1;

//区分大小写,0为大小写一样,1为区分大小写 (好像不起作用) private static int c = 0;

public MySort() { ; } public MySort(int n, int c) { if (n == 1 || n == 0) { MySort.n = n; } else { throw new ArgumentException("非法的排序方式,仅支持0和1"); }

if (c == 1 || c == 0) { MySort.c = c; } else { throw new ArgumentException("非法的参数传递,仅支持0和1"); } }

//重载IComparer中的“Compare”的方法 int IComparer.Compare(object x, object y) { if (MySort.n == 0) { if (MySort.c == 1) { return Comparer.Default.Compare(x, y); } else { return (new CaseInsensitiveComparer()).Compare(x, y); } } else { if (MySort.c == 1) { return Comparer.Default.Compare(y, x); } else { return (new CaseInsensitiveComparer()).Compare(y, x); } } } }

搜索更多相关主题的帖子: MyHash ArrayList Add new 
2005-04-11 17:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-15375-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.604853 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved