很多文章都有写到Hashtable有内部的排序机制,如果要自定义排序的话就要自己写算法来实现的:听起来很抽象,我一向喜欢简单实用的东西,我下面就来总结总结怎样来实现自定义排序Hashtable.
先看看普通的Hashtable的基本实现:
public static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("key1", "value1");
ht.Add("key2", "value2");
ht.Add("key3", "value3");
ht.Add("key4", "value4");
ht.Add("key5", "value5");
foreach (string str in ht.Keys)
{
Console.WriteLine(str + ":" + ht[str]);
}
}
{
Hashtable ht = new Hashtable();
ht.Add("key1", "value1");
ht.Add("key2", "value2");
ht.Add("key3", "value3");
ht.Add("key4", "value4");
ht.Add("key5", "value5");
foreach (string str in ht.Keys)
{
Console.WriteLine(str + ":" + ht[str]);
}
}
运行的结果:
产生这个结果的原因大家都知道,Hashtable内部的排序机制使然.
下面我来说说在平时的遇到的几种排序类型以及实现:
一、我按什么顺序加进去就按什么顺序输出:
public class NoSortHashTable : Hashtable
{
private ArrayList list = new ArrayList();
public override void Add(object key, object value)
{
base.Add(key, value);
list.Add(key);
}
public override void Clear()
{
base.Clear();
list.Clear();
}
public override void Remove(object key)
{
base.Remove(key);
list.Remove(key);
}
public override ICollection Keys
{
get
{
return list;
}
}
这里注意:ArrayList是不排序的(添加的顺序就是输出的顺序)。让它和hashtable结合不就实现这种功能的吗?这样继承了Hashtable具有Hashtable的丰富功能,又满足ArrayList不排序的功能。满足我们的要求。
{
private ArrayList list = new ArrayList();
public override void Add(object key, object value)
{
base.Add(key, value);
list.Add(key);
}
public override void Clear()
{
base.Clear();
list.Clear();
}
public override void Remove(object key)
{
base.Remove(key);
list.Remove(key);
}
public override ICollection Keys
{
get
{
return list;
}
}
public static void Main()
{
NoSortHashTable ht = new NoSortHashTable();
ht.Add("key1", "value1");
ht.Add("key2", "value2");
ht.Add("key3", "value3");
ht.Add("key4", "value4");
ht.Add("key5", "value5");
foreach (string str in ht.Keys)
{
Console.WriteLine(str + ":" + ht[str]);
}
}
这样一运行就满足我的要求了:{
NoSortHashTable ht = new NoSortHashTable();
ht.Add("key1", "value1");
ht.Add("key2", "value2");
ht.Add("key3", "value3");
ht.Add("key4", "value4");
ht.Add("key5", "value5");
foreach (string str in ht.Keys)
{
Console.WriteLine(str + ":" + ht[str]);
}
}
成功了!
二、我按Hashtable中键的大小顺序进行排序
实际上是按照每一个字符的ASCII的值就行排序的。从左到右比较每个字符的Ascii的值,直到满足两个字符的ASCII的值不同即停止比较
public static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("ee", "value1");
ht.Add("dd", "value2");
ht.Add("cc", "value3");
ht.Add("bb", "value4");
ht.Add("aa", "value5");
ArrayList list = new ArrayList(ht.Keys);
list.Sort();
foreach (string str in list)
{
Console.WriteLine(str+":"+ht[str]);
}
}
运行效果:{
Hashtable ht = new Hashtable();
ht.Add("ee", "value1");
ht.Add("dd", "value2");
ht.Add("cc", "value3");
ht.Add("bb", "value4");
ht.Add("aa", "value5");
ArrayList list = new ArrayList(ht.Keys);
list.Sort();
foreach (string str in list)
{
Console.WriteLine(str+":"+ht[str]);
}
}
成功了!
三,我按Hashtable中的值得大小就行排序
原理同上:实际上是按照每一个字符的ASCII的值就行排序的。从左到右比较每个字符的Ascii的值,直到满足两个字符的ASCII的值不同即停止比较
public static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("a", "3");
ht.Add("b", "4");
ht.Add("c", "2");
ht.Add("d", "1");
ArrayList list = new ArrayList(ht.Values);
list.Sort();
foreach (string svalue in list)
{
IDictionaryEnumerator ide = ht.GetEnumerator();
while (ide.MoveNext())
{
if (ide.Value.ToString() == svalue)
{
Console.WriteLine(ide.Key + ":" + svalue);
}
}
}
}
运行效果:{
Hashtable ht = new Hashtable();
ht.Add("a", "3");
ht.Add("b", "4");
ht.Add("c", "2");
ht.Add("d", "1");
ArrayList list = new ArrayList(ht.Values);
list.Sort();
foreach (string svalue in list)
{
IDictionaryEnumerator ide = ht.GetEnumerator();
while (ide.MoveNext())
{
if (ide.Value.ToString() == svalue)
{
Console.WriteLine(ide.Key + ":" + svalue);
}
}
}
}
成功了!
针对第二,第三,我们可以看出来了通过下面的这个方法把Hashtable的键(keys)或值(values)转换成Arraylist.
ArrayList list = new ArrayList(ht.Values);
ArrayList list= new ArrayList(ht.Keys);
这样就可以把Hashtable的排序转换成ArrayList的排序了!ArrayList list= new ArrayList(ht.Keys);
另外ArrayList提供的很多方法排序:
ArrayList.Sort()-------------------按字符的Ascii的值排序
ArrayList.Reverse()---------------反转数组
等还多ArrayList方法。如果都不满足你要的排序功能的话,那就自己针对ArrayList这个数组写算法就能对ArrayList排序,ArrayList排序也就完成了Hashtable的排序
另外说明一点:下面这个方法还支持自定义对象类型的排序
list.Sort(IComparer comparer)
实际上这是策略模式(Strategy)在.net框架类中的体现.下面我举例说说它的用法:1.申明一个自定义的类:
public class Eployee
{
public string name;
public int age;
public string sex;
}
2.实例化3个对象并放进ArrayList数组中去。{
public string name;
public int age;
public string sex;
}
由于对ArrayList数组中的对象按年龄大小进行排序。默认是不支持的。所以需要再定义一个排序的算法类实现IComparer接口,来重新定义排序的算法.也就是实现了动态的修改算法(策略模式)
public static void Main()
{
Eployee ep1 = new Eployee();
ep1.name = "Charles";
ep1.age = 21;
ep1.sex = "男";
Eployee ep2 = new Eployee();
ep2.name = "Sun";
ep2.age = 43;
ep2.sex = "男";
Eployee ep3 = new Eployee();
ep3.name = "Sunny";
ep3.age = 18;
ep3.sex = "男";
ArrayList EmployeeList = new ArrayList();
EmployeeList.Add(ep1);
EmployeeList.Add(ep2);
EmployeeList.Add(ep3);
EmployeeList.Sort(new myEmployeeCompare());
}
实现IComparer接口的类MyEmployeeCompare如下:{
Eployee ep1 = new Eployee();
ep1.name = "Charles";
ep1.age = 21;
ep1.sex = "男";
Eployee ep2 = new Eployee();
ep2.name = "Sun";
ep2.age = 43;
ep2.sex = "男";
Eployee ep3 = new Eployee();
ep3.name = "Sunny";
ep3.age = 18;
ep3.sex = "男";
ArrayList EmployeeList = new ArrayList();
EmployeeList.Add(ep1);
EmployeeList.Add(ep2);
EmployeeList.Add(ep3);
EmployeeList.Sort(new myEmployeeCompare());
}
public class myEmployeeCompare : IComparer
{
#region IComparer Members
public int Compare(object x, object y)
{
return (x as Eployee).age - (y as Eployee).age;
#endregion
}
}
这样就实现了按年龄大小从小到大进行排序。{
#region IComparer Members
public int Compare(object x, object y)
{
return (x as Eployee).age - (y as Eployee).age;
#endregion
}
}
最后说明一点:
针对Hashtable排序的特殊要求,可以先把它的键或值转化成ArrayList,针对ArrayList进行排序,进而也就实现了Hashtable的排序(上面的第二,第三就体现了这一点).毕竟ArrayList已经支持一些排序,而且还支持自定义类型的排序(Strategy),当然还可以自己写算法来实现排序.