|
-
April 6th, 2009, 09:00 AM
#1
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?
-
April 6th, 2009, 09:05 AM
#2
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
April 6th, 2009, 09:18 AM
#3
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());
-
April 6th, 2009, 09:32 AM
#4
Re: Get the Maximum Value from a List
Thank you very much, it was very helpful.
-
April 6th, 2009, 09:40 AM
#5
Re: Get the Maximum Value from a List
 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 
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());
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|