简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM