Is it possible to sort a HashTable?

前端 未结 11 717
不知归路
不知归路 2021-01-17 10:13

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.

相关标签:
11条回答
  • 2021-01-17 11:02

    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.

    0 讨论(0)
  • 2021-01-17 11:05

    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.

    0 讨论(0)
  • 2021-01-17 11:06

    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.

    0 讨论(0)
  • 2021-01-17 11:10

    I am quite sure that hash tables cannot be sorted ... ;)

    Wikipedia Hash Table

    0 讨论(0)
  • 2021-01-17 11:12

    Sorry, but you can't sort hashtable. You will have to refactor your code to use some sortable collections.

    0 讨论(0)
提交回复
热议问题