Project Summary-hashtable sorting

Source: Internet
Author: User

I. Business table analysis:


public static void Main(){Hashtable ht = new Hashtable();ht.Add("key1", "value1"); ht.Add("key2", "value2");ht.Add("key3", "value3");ht.Add("key4", "value4"); ht.Add("key5", "value5"); foreach (string str in ht.Keys) {Console.WriteLine(str + ":" + ht[str]);}}

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

Note:: ArrayList is not sorted (

public static void Main(){NoSortHashTable ht = new NoSortHashTable();ht.Add("key1", "value1");ht.Add("key2", "value2");ht.Add("key3", "value3");ht.Add("key4", "value4");ht.Add("key5", "value5");foreach (string str in ht.Keys){Console.WriteLine(str + ":" + ht[str]);}}

2. sort by the size order of the key in Hashtable.

public static void Main(){Hashtable ht = new Hashtable();ht.Add("ee", "value1");ht.Add("dd", "value2");ht.Add("cc", "value3");ht.Add("bb", "value4");ht.Add("aa", "value5");ArrayList list = new ArrayList(ht.Keys);list.Sort();foreach (string str in list){Console.WriteLine(str+":"+ht[str]);}}

3. I will sort by the value in Hashtable.

public static void Main(){Hashtable ht = new Hashtable();ht.Add("a", "3");ht.Add("b", "4");ht.Add("c", "2");ht.Add("d", "1");ArrayList list = new ArrayList(ht.Values);list.Sort();foreach (string svalue in list){IDictionaryEnumerator ide = ht.GetEnumerator();while (ide.MoveNext()){if (ide.Value.ToString() == svalue){Console.WriteLine(ide.Key + ":" + svalue);}}}}

V. Summary:

Vi. Key notes:

public static void Main(){Hashtable hs = new Hashtable(); hs.Add(5, 40); hs.Add(4, 20); hs.Add(3, 20); hs.Add(2, 15); hs.Add(1, 5); ArrayList als = new ArrayList(hs.Values); als.Sort(); foreach (object ob in als) { IDictionaryEnumerator ide = hs.GetEnumerator(); while (ide.MoveNext()) { if (ide.Value.ToString() == ob.ToString()) Console.WriteLine(ide.Key.ToString() + ":" + ob.ToString()); }} } 

You can change the program:

public static void Main(){Hashtable ht = new Hashtable(); ht.Add(5, 40); ht.Add(4, 20); ht.Add(3, 20); ht.Add(2, 15); ht.Add(1, 5); ArrayList listTemp = new ArrayList(ht.Values); ArrayList list = new ArrayList(); foreach (object value in listTemp) { if (!list.Contains(value)) { list.Add(value); } } list.Sort(); foreach (object obj in list) { IDictionaryEnumerator ide = ht.GetEnumerator(); while (ide.MoveNext()) { if (ide.Value.ToString() == obj.ToString()) { Console.WriteLine(ide.Key.ToString() + ":" + obj.ToString()); } } }} 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.