Dotnet: is there any way out to directly get a reference to collect values in a dictionary?
I want to sort a dictionary based on the value of each dictionanry item.but if I use a sorted dictionary the search time will increase from constant to log2 (n) .so I want to directly assign a list of values in the dictionary link to the list. then i can sort that list and get results.i don't want to iterate over each element of the dictionary to add its value to the list, which will increase the complexity?
0
a source to share
1 answer
You can get a collection of values by property Dictionary<>.Values
. In the following example, you are not looping the values, but the framework does it for you.
Dictionary<int, Item> items = new Dictionary<int, Item>();
List<Item> values = new List<Item>(items.Values);
values.Sort();
You can also keep another value-only list and you can add your items to both the dictionary and the list.
+2
a source to share