zoukankan      html  css  js  c++  java
  • Hashtable 排序

    Hashtable本身并没有排序功能,相对来说,它的主要优点在于快速查找。
    但有的时候我们也需要对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]);
                }

            }

        }

    }

    程序运行结果见图一:
           
    排序完成了,但是像图二那样,要实现反向排序(倒序)该如何操作?
    好像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;
            }

        }

    }

    程序运行结果见图二。
    说 明:  
       如果你想实现升序,只需要把
       r = -((IComparable)a.Key).CompareTo(b.Key); 前面的负号去掉, 
       改为: r = ((IComparable)a.Key).CompareTo(b.Key); 
       (注:以上代码得到了 破宝 的指导)
  • 相关阅读:
    react redux 使用
    github 退出和别人共同开发的仓库
    在react package.json中配置代理解决跨域
    禁止浏览器sources下webpack文件 显示源码
    redux connect 装饰器配置和安装
    Odoo 在action的domain和context中使用self.env
    odoo 字段后面添加button按钮,页签tree再加group显示字段
    Odoo -- 开发者模式创建的群组、动作没有xml id怎么办
    Mac必备神器Homebrew mac下镜像飞速安装Homebrew教程
    Vue -- keyup、watch、computed、nrm的安装
  • 原文地址:https://www.cnblogs.com/publicbill/p/253317.html
Copyright © 2011-2022 走看看