c# - Get keys for maximum value from dictionary -


i have dictionary of type

dictionary<int, gvalue>  

where gvalue object contains 2 double values p1 , p2

i trying key maximum values dictinary using follwing code

var keyformaxp1 = dict.keys.aggregate((i, j) => dict[i].p1 >= dict[j].p1 ? : j); var keyformaxp2 = dict.keys.aggregate((i, j) => dict[i].p2 >= dict[j].p2 ? : j); 

it gives me keys max p1 , p2 correctly.

but if dictinary contains more 1 key max p1 or p2 value ?? here still returning 1 key comes first during traversing of dictinary.

edit

suppose dictionary have values p1 max value 3.52. if there 2 entries in dictionary value p1 = 3.52, wanna both keys related value

if want keyvalupairs max-value can use enumerable.groupby:

var maxp1keyvalues = dict.groupby(kv => kv.value.p1)     .orderbydescending(g => g.key).first(); var maxp2keyvalues = dict.groupby(kv => kv.value.p2)     .orderbydescending(g => g.key).first();  foreach (var kv in maxp1keyvalues) {     console.writeline("key:{0} value-p1:{1}", kv.key, kv.value.p1); } foreach (var kv in maxp2keyvalues) {     console.writeline("key:{0} value-p2:{1}", kv.key, kv.value.p2); } 

this groups keyvaluepair<int, gvalue> in dictionary value of p1/p2, orderbydescending + first selects group highest values.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -