您的位置:首页 > 其它

利用ArrayList对Hashtable其进行排序

2013-12-04 21:14 323 查看

前言:

最近在使用Hashtable的时候发现一个问题:就是当你对Hashtable进行遍历的时候整个输出结果是毫无顺序的,

上网查了一下说是Hashtable有自己内部的排序机制,如果要自定义排序的话就要写算法,反正是很麻烦,而我现在

需要实现的仅仅是输入顺序和输入顺序对应即可,没必要这么麻烦的去写算法。

这里我们就需要使用到ArrayList,大家都知道ArrayList默认是不排序的(添加的顺序就是输出的顺序)。让它
和hashtable结合不就实现这种功能的吗?这样继承了Hashtable具有Hashtable的丰富功能,又满足ArrayList不排序
的功能。上网查了一些资料,顺便也介绍一些其他的排序方法,大家一起学习。
没有排序之前:

using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable map = new Hashtable();
            map.Add("TableName", "T_English_Close");
            map.Add("QuestionID", "10");
            map.Add("Scope", "课内");
            map.Add("BookName", "第一册");
            map.Add("Chapter", "第一章");
            map.Add("QuestionType", "完型填空");
            map.Add("Degree", "2");
            map.Add("Fraction", "10");
            foreach (string str in map.Keys)
            {
                Console.WriteLine(str + " : " +map[str]); 
            }
            Console.Read();
        }
    }
}


输出结果:



一:输入什么顺序,输出就是什么顺序。

首先我们需要声明一个类来继承Hashtable,并重新它的一些方法,使ArrayList与Hashtable结合在一起:

public class AltHashTable : 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;
        }
    }
}


然后我们重新测试一下:

using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
             AltHashTable map= newAltHashTable(); 

            map.Add("TableName", "T_English_Close");
            map.Add("QuestionID", "10");
            map.Add("Scope", "课内");
            map.Add("BookName", "第一册");
            map.Add("Chapter", "第一章");
            map.Add("QuestionType", "完型填空");
            map.Add("Degree", "2");
            map.Add("Fraction", "10");
            foreach (string str in map.Keys)
            {
                Console.WriteLine(str + " : " +map[str]); 
            }
            Console.Read();
        }
    }
}


输出结果正是我们想要的:





二:下面我们在分别介绍一下按Key值和Value值排序的方法。

首先要知道如何将Hashtable转换为ArrayList:

ArrayList list = new ArrayList(ht.Values);
ArrayList list=  new ArrayList(ht.Keys);


按Key值排序:

using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable   map = new Hashtable  ();
            map.Add("TableName", "T_English_Close");
            map.Add("QuestionID", "10");
            map.Add("Scope", "课内");
            map.Add("BookName", "第一册");
            map.Add("Chapter", "第一章");
            map.Add("QuestionType", "完型填空");
            map.Add("Degree", "2");
            map.Add("Fraction", "10");
            ArrayList list = new ArrayList(map.Keys);
            list.Sort();
            foreach (string str in list)
            {
                Console.WriteLine(str + ":" + map[str]);
            }
            Console.Read();
 }
    }
}
输出结果:



按Value值排序跟按Key值排序一样,在这里就不在写代码了。大家可以自己动手实践一下!

原理:

实际上是按照每一个字符的ASCII的值就行排序的。从左到右比较每个字符的Ascii的值,直到满足两个字符的ASCII
的值不同即停止比较 。

总结:

针对Hashtable排序的特殊要求,可以先把它的键或值转化成ArrayList,针对ArrayList进行排序,进而也就实现了
Hashtable的排序.毕竟ArrayList已经支持一些排序。而且ArrayList的排序种类非常多,我们在下面文章中在重点说一
下ArrayList的使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: