8

I declare dictionary as following:

private Dictionary<int, touchInformation> touchDictionary = new Dictionary<int, touchInformation>();

And I used as following:

touchDictionary[touchID] = touchObject;

So, the touchDictionary will keep the key from touchID. Now, I try to find the minimum key using dictionary but I don't know how to do. Have any suggestion?

Regard, C.Porawat

Suman Banerjee
  • 1,923
  • 4
  • 24
  • 40
  • Could you explain your use case here? What is the benefit of knowing the min? – Ray Booysen Jan 26 '11 at 09:04
  • 1
    An alternative way of doing this, is to store the "Current" minimum value and on every add/remove from the dictionary, check against this and update if necessary. It'll stop the enumeration of the keys. – Ray Booysen Jan 26 '11 at 09:04
  • 2
    Have you considered if a `SortedList<,>` or `SortedDictionary<,>` is a better fit for your collection? http://msdn.microsoft.com/en-us/library/5z658b67.aspx – Ani Jan 26 '11 at 09:06
  • I'm trying to check boolean value in all object in dictionary. – Porawat Chimcherdsin Jan 26 '11 at 09:18

3 Answers3

24

Dictionary has a Keys property which allows you to enumerate the keys within the dictionary. You can use the Min Linq extension methods to get the minimum key as follows:

int minimumKey = touchDictionary.Keys.Min();
ColinE
  • 68,894
  • 15
  • 164
  • 232
0

Something like touchDictionary.Keys.Min(). Just make sure you import the System.Linq namespace.

marcind
  • 52,944
  • 13
  • 125
  • 111
0

There is a good answer inn this post on SO:

How do you sort a dictionary by value?

You would just need to sort by Key instead

Community
  • 1
  • 1
WraithNath
  • 17,658
  • 10
  • 55
  • 82