+ Start a Discussion
Sascha DeinertSascha Deinert 

Math.Max function


I tried to find the max value of some different value. Late I will replace the numbers with variables.
MaxValue = Math.max (1,4,5,2,10);
Compile Error: Method does not exist or incorrect signature: Math.max(Integer, Integer, Integer, Integer, Integer)

Could you help me please.

Suresh RaghuramSuresh Raghuram

Hi Sascha,

Please refer the above link .
it seems the max function is taking only two arguments in apex coding.
But in formula fields it is taking more than one argument.

If this answers your question make this as solution.

Sascha DeinertSascha Deinert
Thanks for your respone, you are right, you can only add two values into the function.

Can I add the values into an array and than I use the math.max function?
Integer[] myList = new List<Integer> { 1,4,5,2,10 };
MaxValue = math.max(myList);
I tried, but I geht an error:

Method does not exist or incorrect signature: math.max(List<Integer>) 

If this is not possible, how can I find the max value of an array or different variables?

Bhanu MaheshBhanu Mahesh
Hi Sascha,

If you want to find the maximum or minimum from a list, sort  the list first. Default sorting order is ASC. 
First element will be minimum and last element in the list will be maximum

Integer[] myList = new List<Integer> { 1,4,5,2,10 };
MaxValue = mylist[4];
MinValue = mylist[0];

Bhanu Mahesh