Sorting a Dictionary in place with respect to keys

前端 未结 7 1253
广开言路
广开言路 2020-11-29 04:49

I have a dictionary in C# like

Dictionary

and I want to sort that dictionary in place with respect to keys (a f

相关标签:
7条回答
  • 2020-11-29 05:44

    While Dictionary is implemented as a hash table, SortedDictionary is implemented as a Red-Black Tree.

    If you don't take advantage of the order in your algorithm and only need to sort the data before output, using SortedDictionary would have negative impact on performance.

    You can "sort" the dictionary like this:

    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    // algorithm
    return new SortedDictionary<string, int>(dictionary);
    
    0 讨论(0)
提交回复
热议问题