Click here to Skip to main content
15,895,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have hash table like below...
C#
public Hashtable BindDD()
    {
        Hashtable objDd = new Hashtable();
        objDd.Add("0", "DD");
        objDd.Add("1", "1");
        objDd.Add("2", "2");
        objDd.Add("3", "3");
        objDd.Add("4", "4");
        objDd.Add("5", "5");
        objDd.Add("6", "6");
        objDd.Add("7", "7");
        objDd.Add("8", "8");
        objDd.Add("9", "9");
        objDd.Add("10", "10");
        objDd.Add("11", "11");
        objDd.Add("12", "12");
        objDd.Add("13", "13");
        objDd.Add("14", "14");
        objDd.Add("15", "15");
        objDd.Add("16", "16");
        objDd.Add("17", "17");
        objDd.Add("18", "18");
        objDd.Add("19", "19");
        objDd.Add("20", "20");
        objDd.Add("21", "21");
        objDd.Add("22", "22");
        objDd.Add("23", "23");
        objDd.Add("24", "24");
        objDd.Add("25", "25");
        objDd.Add("26", "26");
        objDd.Add("27", "27");
        objDd.Add("28", "28");
        objDd.Add("29", "29");
        objDd.Add("30", "30");
        objDd.Add("31", "31");
        return objDd;
    }

i am binding this return value to dropdown

C#
public void BindDD()
   {
       Hashtable ht = (new GlobalConstants().BindDD());
       ddlDD.DataSource = ht;
       ddlDD.DataTextField = "Value";
       ddlDD.DataValueField = "Key";
       ddlDD.DataBind();
   }


Here is my Problem when i am binding to drop down i am not able bind in order..in which i added to hash table. is it possible to sort the hash table .. Plz help me in solving this problem ..

Thanks in advance..
Posted
Updated 13-Mar-12 17:51pm
v2

If you want sortable, you're using the wrong collection. HashTables are not meant to be sortable. List<t> is. So why not make a small class or structure (depending on requirements) and create a List<t> of that type. Then you can sort it to your hearts content and even provide your own custom sort.
 
Share this answer
 
Comments
Rajesh Yerramsetti 14-Mar-12 0:19am    
Please Provide me with Example related to my requirement
Sergey Alexandrovich Kryukov 14-Mar-12 1:04am    
Good point, a 5.
--SA
You can't sort a HashTable. If you could, it wouldn't be a HashTable. You can enumerate the HashTable, and then sort the enumeration. But that would be very slow. Recommendation would be to use .NET SortedDictionary instead.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-12 1:03am    
Ha-ha, good point, a 5.
--SA
Use Dictionary rather than Hash Table

XML
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(2, "This");
myDict.Add(1, "is");
myDict.Add(5, "radio");
myDict.Add(4, "clash");

List<string> song = new List<string>(myDict.Values);
song.Sort();
http://www.c-sharpcorner.com/uploadfile/prasoonk/hashtable-sorting/[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Mar-12 1:05am    
Good advice, a 5. HashTable is rendered obsolete as per v.2.0, when generics were introduced.
But looks at the answers by Dave and Ganesan.
--SA
Rajesh Yerramsetti 14-Mar-12 1:23am    
but i used the sorted list which is having inbuilt sorting functionality.is there any problem with sorted list
Dave Kreskowiak 14-Mar-12 10:28am    
No, there's no problem with the Sort method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900