Find Minimum (Min) and Maximum (Max) value from Generic List of KeyValuePair using C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

I have a multidimensional array with basically a key / value pair. I would like to sum all the values according to the key then pick the min and max values from this.

I tried making a List<keyvaluepair> and a dictionary to do this I got very close but it wouldn’t let me sort through a list of doubles. My lists and arrays are created correctly without issues. I would like to display the min/max of value and also display the key in the same line.

        public static void PrintTable()
        {
            string text;
            var totals = new List<KeyValuePair<string, double>>();
            for(int i = 0; i < topics.Length; i++)
            {
                Console.Write(topics[i]  + " ");
                double num = 0;
                for(int j = 0; j < 10; j++)
                {
                    Console.Write(responses[i, j] + " ");
                    num += responses[i, j] * (j + 1);
                    totals.Add(new KeyValuePair<string, double>( topics[i], num));
                }
                average = num / userCounter;
                Console.Write("\t" + average.ToString("##.00"));
            }
            // Display highest points and lowest points.
            var addedSums = totals.GroupBy(k => k.Key).ToDictionary(a => a.Key, b => b.Sum(c => c.Value));
            foreach(var pair in addedSums.Values.OrderByDescending())
            {
                Console.WriteLine("Highest points: " + pair);
            }
        }

 

This question does not have replies that have been liked.