I have a property that returns a HashTable
. I would like to sort it without refactoring my property. Please note: I do not want to return another type.
I am a new programmer so take everything I say with a grain of salt. But here is what I did when I ran into a similar situation. I created a class that had two variables and then created a List
object off those variables and then I used linq to sort those variables.
lubos is right: 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. Much better to use a SortedDictionary instead.
You will need to return something other than a hash table. I won't reiterate what you claim to understand already, but you need to rethink whatever part of your design requires you to return sorted objects in a hash table.
I am quite sure that hash tables cannot be sorted ... ;)
Wikipedia Hash Table
Sorry, but you can't sort hashtable. You will have to refactor your code to use some sortable collections.