2
Reply

Get MAX and MIN Values in ArrayList

Eline Sami

Eline Sami

Jul 9 2014 5:17 AM
10.6k
I have an ArrayList that contains some double values and I need to get the MAX and MIN values.

Also, I need to sort this ArrayList. How can I do that?

myCode is below. It doesn't give the right Max value for example when I have like three numbers in my arraylist and all are negative. The max value is being returned is Zero.

ArrayList Values = new ArrayList();
double Max = 0; double Min =0; double temp = 0;

foreach (double x in Values)
{
temp = x;
if(Max<temp)
{
Max = temp;
}
if(Min > temp)
{
Min = temp;
}
}

Answers (2)