hashtable和arraylist(转载)

实现在外部给hashtable排序

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MyClassLibrary
{
    public class MyHashtable : Hashtable
    {
        private ArrayList _MyKeys = new ArrayList();

        public MyHashtable()
        {
        }

        public override void Add(object key, object value)
        {
            base.Add(key, value);
            _MyKeys.Add(key);
        }

        public override ICollection Keys
        {
            get
            {
                return Keys;
            }
        }

        public ICollection MyKeys
        {
            get
            {
                return _MyKeys;
            }
        }

        public override void Clear()
        {
            base.Clear();
            _MyKeys.Clear();
        }

        public override void Remove(object key)
        {
            base.Remove(key);
            _MyKeys.Remove(key);
        }
        public override IDictionaryEnumerator GetEnumerator()
        {
            return base.GetEnumerator();
        }
    }
}

 

    private void SortHashtable()
    {
        MyHashtable ht = new MyHashtable();

        ht.Add(DateTime.Now.AddDays(1).ToString(), "1");
        ht.Add(DateTime.Now.AddDays(2).ToString(), "2");
        ht.Add(DateTime.Now.AddDays(3).ToString(), "3");
        ht.Add(DateTime.Now.AddDays(4).ToString(), "4");
        ht.Add(DateTime.Now.AddDays(5).ToString(), "5");

        foreach (string str in ht.MyKeys)
        {
            this.DropDownList1.Items.Add(new ListItem(str, ht[str].ToString()));
        } 
        this.DropDownList1.DataBind();
    }

 

posted on 2012-07-17 10:15  小黑混北京  阅读(196)  评论(0编辑  收藏  举报