views:

84

answers:

1

i want to sort dictionary based upon value of each dictioanry item.but if i use sorted dictionary search time complexity will increase from constant to log2(n).so i want to directly assign reference of value list in dictionary to a list.then i can sort this list and get results.i dont want to iterate to each element of dictionary to add its value in list that will increase complexity?

+2  A: 

You can get values collection by Dictionary<>.Values property. In the following example, you don't loop through values but 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 list just for values and you can add your items both into dictionary and list.

idursun