Hashtable本身并没有排序功能,相对来说,它的主要优点在于快速查找。
但有的时候我们也需要对Hashtable里面的元素进行排序,这就需要变通的方法来实现。
大家都知道:ArrayList它有一个Sort()方法,可以将里面的元素进行排序,试想如果将Hashtable里面的元素导入到ArrayList里面,然后再进行排序,这倒是一个不错的想法,现在我们加以实现:
程序运行结果见图一:
排序完成了,但是像图二那样,要实现反向排序(倒序)该如何操作?
好像ArrayList里面也就仅仅提供了一个Sort()方法,没有提供给我们直接倒序的方法,
废话少说,见如下代码:
程序运行结果见图二。
但有的时候我们也需要对Hashtable里面的元素进行排序,这就需要变通的方法来实现。
大家都知道:ArrayList它有一个Sort()方法,可以将里面的元素进行排序,试想如果将Hashtable里面的元素导入到ArrayList里面,然后再进行排序,这倒是一个不错的想法,现在我们加以实现:
using System;
using System.Collections;
namespace PublicBill.HashtableSort
{
public class HashtableSort
{
public static void Main()
{
string myString = "1,AAA;2,BBB;3,CCC";
string[] myStringArray = myString.Split(';');
string[] mySubStringArray;
Hashtable ht = new Hashtable();
foreach(string str in myStringArray)
{
mySubStringArray = str.Split(',');
ht.Add(mySubStringArray[0], mySubStringArray[1]);
}
Console.WriteLine("Hashtable before sort.");
foreach(DictionaryEntry de in ht)
{
Console.WriteLine("{0}: {1}", de.Key, de.Value);
}
ArrayList al = new ArrayList(ht.Keys);
al.Sort();
Console.WriteLine();
Console.WriteLine("Hashtable after sort.");
foreach(string key in al)
{
Console.WriteLine("{0}: {1}", key, ht[key]);
}
}
}
}
using System.Collections;
namespace PublicBill.HashtableSort
{
public class HashtableSort
{
public static void Main()
{
string myString = "1,AAA;2,BBB;3,CCC";
string[] myStringArray = myString.Split(';');
string[] mySubStringArray;
Hashtable ht = new Hashtable();
foreach(string str in myStringArray)
{
mySubStringArray = str.Split(',');
ht.Add(mySubStringArray[0], mySubStringArray[1]);
}
Console.WriteLine("Hashtable before sort.");
foreach(DictionaryEntry de in ht)
{
Console.WriteLine("{0}: {1}", de.Key, de.Value);
}
ArrayList al = new ArrayList(ht.Keys);
al.Sort();
Console.WriteLine();
Console.WriteLine("Hashtable after sort.");
foreach(string key in al)
{
Console.WriteLine("{0}: {1}", key, ht[key]);
}
}
}
}
程序运行结果见图一:
排序完成了,但是像图二那样,要实现反向排序(倒序)该如何操作?
好像ArrayList里面也就仅仅提供了一个Sort()方法,没有提供给我们直接倒序的方法,
废话少说,见如下代码:
using System;
using System.Collections;
namespace PublicBill.HashtableSort
{
public class HashtableSort
{
public static void Main()
{
string myString = "1,AAA;2,BBB;3,CCC";
string[] myStringArray = myString.Split(';');
string[] mySubStringArray;
Hashtable ht = new Hashtable();
foreach(string str in myStringArray)
{
mySubStringArray = str.Split(',');
ht.Add(mySubStringArray[0], mySubStringArray[1]);
}
Console.WriteLine("Hashtable before sort.");
foreach(DictionaryEntry de in ht)
{
Console.WriteLine("{0}: {1}", de.Key, de.Value);
}
ArrayList al = new ArrayList(ht);
CustomComparer comparer = new CustomComparer();
al.Sort(comparer);
Console.WriteLine(); //Print a empty line.
Console.WriteLine("Hashtable after order by desc.");
foreach(DictionaryEntry de in al)
{
Console.WriteLine("{0}: {1}", de.Key, de.Value);
}
}
}
public class CustomComparer : IComparer
{
public int Compare(object x, object y)
{
int r = 0;
if (x is DictionaryEntry && y is DictionaryEntry)
{
DictionaryEntry a = (DictionaryEntry)x;
DictionaryEntry b = (DictionaryEntry)y;
if (a.Key is IComparable)
{
//If you want sort asc,only need remove "-".
r = -((IComparable)a.Key).CompareTo(b.Key);
}
}
return r;
}
}
}
using System.Collections;
namespace PublicBill.HashtableSort
{
public class HashtableSort
{
public static void Main()
{
string myString = "1,AAA;2,BBB;3,CCC";
string[] myStringArray = myString.Split(';');
string[] mySubStringArray;
Hashtable ht = new Hashtable();
foreach(string str in myStringArray)
{
mySubStringArray = str.Split(',');
ht.Add(mySubStringArray[0], mySubStringArray[1]);
}
Console.WriteLine("Hashtable before sort.");
foreach(DictionaryEntry de in ht)
{
Console.WriteLine("{0}: {1}", de.Key, de.Value);
}
ArrayList al = new ArrayList(ht);
CustomComparer comparer = new CustomComparer();
al.Sort(comparer);
Console.WriteLine(); //Print a empty line.
Console.WriteLine("Hashtable after order by desc.");
foreach(DictionaryEntry de in al)
{
Console.WriteLine("{0}: {1}", de.Key, de.Value);
}
}
}
public class CustomComparer : IComparer
{
public int Compare(object x, object y)
{
int r = 0;
if (x is DictionaryEntry && y is DictionaryEntry)
{
DictionaryEntry a = (DictionaryEntry)x;
DictionaryEntry b = (DictionaryEntry)y;
if (a.Key is IComparable)
{
//If you want sort asc,only need remove "-".
r = -((IComparable)a.Key).CompareTo(b.Key);
}
}
return r;
}
}
}
程序运行结果见图二。
说 明: 如果你想实现升序,只需要把 r = -((IComparable)a.Key).CompareTo(b.Key); 前面的负号去掉, 改为: r = ((IComparable)a.Key).CompareTo(b.Key); (注:以上代码得到了 破宝 的指导) |