Assuming you have a Dictionary<TKey, TValue>
and want to find the kth largest key:
main.cs388 chars13 lines
Explanation:
We first check if the dictionary contains at least k elements. If it doesn't, we print a message saying how many elements the dictionary has.
Assuming the dictionary has at least k elements, we order the dictionary by key in descending order, and then get the element at index k-1 (since indexes are 0-based).
The ElementAt
method is used to avoid sorting the entire dictionary unnecessarily. If you want to get all the keys sorted, you can replace ElementAt
with ToList
to get a sorted list of all the keys.
main.cs87 chars2 lines
gistlibby LogSnag