Porawat Chimcherdsin
Porawat Chimcherdsin

Reputation: 219

How to find minimum key in dictionary

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

Upvotes: 8

Views: 17243

Answers (3)

WraithNath
WraithNath

Reputation: 18013

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

Upvotes: 0

marcind
marcind

Reputation: 53183

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

Upvotes: 0

ColinE
ColinE

Reputation: 70122

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();

Upvotes: 24

Related Questions