0

I have a dictionary which is sorted in descending order.Each string (key) is a term and the int (value) its count. How I get the first count? because it refers to the maximum count (frequency)....... thanks in advance.

Just to inform some of whom commented that the dictionary<string,int> will change its
rank. Be sure, if you are grouping  the dictionary by its count, there is no problem  
with the order . Always the dictionary will come with highest count at first.
FSm
  • 2,017
  • 7
  • 29
  • 55
  • 1
    `dict.First()`?! http://stackoverflow.com/a/436957/284240 You might want to use an [`OrderedDictionary `](http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx) instead – Tim Schmelter May 08 '12 at 11:07
  • Dictionaries are are hash tables. They are not sorted. Show us some code. – Steven May 08 '12 at 11:07
  • @Tim Schmelter u mean Dict.value .... right ? – FSm May 08 '12 at 11:09
  • @steaven, even it is hash table but my dic is sorted. – FSm May 08 '12 at 11:11
  • @Qaesar how is your dictionary sorted? Are you wanting to get the row with the highest value (i.e. sort by value, not key)? – Dave May 08 '12 at 11:12
  • I'm guessing the sort is by value which is why you want the first item? – Chris Gessler May 08 '12 at 11:15

4 Answers4

7

What do you mean by 'dictionary sorted in descending order'? A Dictionary<TKey,TValue> by definition is unsorted! Do you mean SortedDictionary<TKey,TValue>? If so, you can use:

var firstCount = sortedDictionary.First().Value;
thecoop
  • 45,220
  • 19
  • 132
  • 189
1

You cannot depend on a Dictionary to stay ordered (except it is an OrderedDictionary of course). If you are using an OrderedDictionary, you can use its indexer:

var maximumCount = myDictionary[0];

or

var maximumCount = myDictionary.First().Value;

EDIT: If you want the highest count in the entire dictionary, you could also just use this:

var maximumCount = myDictionary.Max(entry => entry.Value);
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • @ Botz, you are right, but I already put each Maxcount for each returned dic in array[int]. thanks for the notice. – FSm May 08 '12 at 11:27
0

What about the below

yourDictionary.First().Value

bear in mind that as you add more values it may very well change order

MSDN warns about that

http://msdn.microsoft.com/en-us/library/ekcfxy3x(v=vs.100).aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

I believe you're using the wrong type of Dictionary. Use an OrderedDictionary<> instead. It will provide you with a guaranteed order and an indexer.

OrderedDictionary list = new OrderedDictionary();

// add a bunch of items

int firstValue = (int)list[0];

The only drawback with an OrderedDictionary is that it's not generic, but here's how you can make it generic. http://www.codeproject.com/Articles/18615/OrderedDictionary-T-A-generic-implementation-of-IO

Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • nothing wrong i think. my dictionary always come with highest in the first – FSm May 08 '12 at 11:20
  • @Qaesar - the Dictionary<> collection's order is not guaranteed to be in the same order as when you added items. It may seem like it simply because it's working now, but out in production it may fail to produce the proper result. I hope you're not writing a stock ticker or any type of financial product. – Chris Gessler May 08 '12 at 11:33
  • haha... no it is not type of financial product. but believe me it is not problem, because I'm returning all value of dic one time. – FSm May 08 '12 at 11:40