Get the Maximum Value from a List
Well guys, I have variable-sized List and I have populated it with double values...
List<double> tempAvg = new List<double>();
I want to know the Maximum value within the List. I thought first to sort the List and then get the last index, but I dont know how to do that.
any Idea?
Re: Get the Maximum Value from a List
There is Array.Sort() method, if you are using fx3.5 with LINQ, you can use Max() extension method directly.
Re: Get the Maximum Value from a List
Use the Sort method that built into the List. You will have to do a custom sor to get the list in the descending order, something like this
Code:
List<double> tempAvg = new List<double>();
tempAvg.Add(12.90);
tempAvg.Add(2.90);
tempAvg.Add(13.90);
tempAvg.Add(1.90);
tempAvg.Sort(delegate(double d1, double d2)
{
return d2.CompareTo(d1);
});
Console.WriteLine(tempAvg[0].ToString());
Re: Get the Maximum Value from a List
Thank you very much, it was very helpful.
Re: Get the Maximum Value from a List
Quote:
Originally Posted by
boudino
There is Array.Sort() method, if you are using fx3.5 with LINQ, you can use Max() extension method directly.
I just tried the extension method in 3.5 and it works like a charm. Thanks :thumb:
Code:
//top of the module
using System.Linq;
//all other code
List<double> tempAvg = new List<double>();
tempAvg.Add(12.90);
tempAvg.Add(2.90);
tempAvg.Add(13.90);
tempAvg.Add(1.90);
Console.WriteLine(tempAvg.Max().ToString());