Hashtable 排序

Hashtable本身并没有排序功能,相对来说,它的主要优点在于快速查找。
但有的时候我们也需要对Hashtable里面的元素进行排序,这就需要变通的方法来实现。
大家都知道:ArrayList它有一个Sort()方法,可以将里面的元素进行排序,试想如果将Hashtable里面的元素导入到ArrayList里面,然后再进行排序,这倒是一个不错的想法,现在我们加以实现:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  PublicBill.HashtableSort
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HashtableSort
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string myString = "1,AAA;2,BBB;3,CCC";
InBlock.gif            
string[] myStringArray = myString.Split(';');
InBlock.gif            
string[] mySubStringArray;
InBlock.gif            Hashtable ht 
= new Hashtable();
InBlock.gif            
foreach(string str in myStringArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mySubStringArray 
= str.Split(',');
InBlock.gif                ht.Add(mySubStringArray[
0], mySubStringArray[1]);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.WriteLine(
"Hashtable before sort.");
InBlock.gif            
foreach(DictionaryEntry de in ht)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"{0}: {1}", de.Key, de.Value);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ArrayList al 
= new ArrayList(ht.Keys);
InBlock.gif            al.Sort();
InBlock.gif            Console.WriteLine();
InBlock.gif
InBlock.gif            Console.WriteLine(
"Hashtable after sort.");
InBlock.gif            
foreach(string key in al)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"{0}: {1}", key, ht[key]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

程序运行结果见图一:
CSharpHashtableSort1.JPG        CSharpHashtableSort2.JPG
排序完成了,但是像图二那样,要实现反向排序(倒序)该如何操作?
好像ArrayList里面也就仅仅提供了一个Sort()方法,没有提供给我们直接倒序的方法,
废话少说,见如下代码: 
None.gif using  System;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  PublicBill.HashtableSort
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HashtableSort
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string myString = "1,AAA;2,BBB;3,CCC";
InBlock.gif            
string[] myStringArray = myString.Split(';');
InBlock.gif            
string[] mySubStringArray;
InBlock.gif            Hashtable ht 
= new Hashtable();
InBlock.gif            
foreach(string str in myStringArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mySubStringArray 
= str.Split(',');
InBlock.gif                ht.Add(mySubStringArray[
0], mySubStringArray[1]);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.WriteLine(
"Hashtable before sort.");
InBlock.gif            
foreach(DictionaryEntry de in ht)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"{0}: {1}", de.Key, de.Value);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ArrayList al 
= new ArrayList(ht);
InBlock.gif            CustomComparer comparer 
= new CustomComparer();
InBlock.gif            al.Sort(comparer);
InBlock.gif
InBlock.gif            Console.WriteLine(); 
//Print a empty line.
InBlock.gif

InBlock.gif            Console.WriteLine(
"Hashtable after order by desc.");
InBlock.gif            
foreach(DictionaryEntry de in al)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"{0}: {1}", de.Key, de.Value);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public class CustomComparer : IComparer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int Compare(object x, object y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int r = 0;
InBlock.gif
InBlock.gif            
if (x is DictionaryEntry && y is DictionaryEntry)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DictionaryEntry a 
= (DictionaryEntry)x;
InBlock.gif                DictionaryEntry b 
= (DictionaryEntry)y;
InBlock.gif            
InBlock.gif                
if (a.Key is IComparable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//If you want sort asc,only need remove "-".
InBlock.gif
                    r = -((IComparable)a.Key).CompareTo(b.Key);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif    
InBlock.gif            
return r;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

程序运行结果见图二。
说 明:  
   如果你想实现升序,只需要把
   r = -((IComparable)a.Key).CompareTo(b.Key); 前面的负号去掉, 
   改为: r = ((IComparable)a.Key).CompareTo(b.Key); 
   (注:以上代码得到了 破宝 的指导)

转载于:https://www.cnblogs.com/publicbill/archive/2005/10/12/253317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值