Click to See Complete Forum and Search --> : Get the Maximum Value from a List
raulbolanos
April 6th, 2009, 09:00 AM
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?
boudino
April 6th, 2009, 09:05 AM
There is Array.Sort() method, if you are using fx3.5 with LINQ, you can use Max() extension method directly.
Shuja Ali
April 6th, 2009, 09:18 AM
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 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());
raulbolanos
April 6th, 2009, 09:32 AM
Thank you very much, it was very helpful.
Shuja Ali
April 6th, 2009, 09:40 AM
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:
//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());
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.