C# Hashtable 正序和逆序排序

來源:互聯網
上載者:User

注意:Hashtable 按鍵的正序排列鍵/值對,預設情況下的遍曆GetValues是逆序遍曆。

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

namespace HashtableSort
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("+++++++++++對HashTable排序測試+++++++++++");
            HashTable htb = new HashTable();
            htb.Add(1, "1");
            htb.Add(3, "3");
            htb.Add(0, "0");
            htb.Add(2, "2");
            htb.Add(7, "7");
            htb.Add(6, "6");
            htb.Add(8, "8");
            htb.Add(10, "10");
            htb.Add(9, "9");
            htb.Add(11, "11");
            htb.GetValues();
            htb.GetInfo();
            htb.AscSort();
            htb.DescSort();
        }
    }
    class HashTable
    {
        Hashtable htb;
        public HashTable()
        {
            htb = new Hashtable();
        }
        //添加鍵/值對
        public void Add(object key, object value)
        {
            htb.Add(key, value);
            Console.WriteLine(string.Format("向Hashtable中添加了/t鍵{0},值{1}", key, value));
        }
        //遍曆Hsahtbale
        public void GetValues()
        {
            Console.WriteLine("+++++++++++遍曆Hsahtbale開始+++++++++++");
            foreach(DictionaryEntry de in htb)
            {
                Console.WriteLine("當前Hashtable中的鍵/值對:/t鍵:{0},值:{1}",de.Key,de.Value);
            }
            Console.WriteLine("+++++++++++遍曆Hsahtbale結束+++++++++++");
        }
        //得到Hashtable資訊
        public void GetInfo()
        {
            Console.WriteLine("Hashtable中有鍵/值對{0}對", htb.Count);
        }
        //對HashTable排序
        public void AscSort()
        {
            Console.WriteLine("+++++++++++對HashTable正序排序開始+++++++++++");
            ArrayList arrList = new ArrayList(htb.Keys);
            arrList.Sort();
            //for (int i = 0; i < arrList.Count; i++)
            //{
            //    Console.WriteLine("/t鍵{0}", arrList[i]);
            //}
            for (int i = 0; i < arrList.Count; i++)
            {
                Console.WriteLine(string.Format("/t鍵:{0}的值是:{1}", arrList[i], htb[arrList[i]]));
            }
            Console.WriteLine("+++++++++++對HashTable正序排序結束+++++++++++");
        }
        public void DescSort()
        {
            Console.WriteLine("+++++++++++對HashTable逆序排序開始+++++++++++");
            ArrayList arrList = new ArrayList(htb.Keys);
            arrList.Sort();
            //for (int i = 0; i < arrList.Count; i++)
            //{
            //    Console.WriteLine("/t鍵{0}", arrList[i]);
            //}
            for (int i = arrList.Count-1; i >= 0; i--)
            {
                Console.WriteLine(string.Format("/t鍵:{0}的值是:{1}", arrList[i], htb[arrList[i]]));
            }
            Console.WriteLine("+++++++++++對HashTable逆序排序結束+++++++++++");
        }
    }
}

有特點的幾處:arrList[i], htb[arrList[i]]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.