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); } } } }