function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sascha DeinertSascha Deinert 

Math.Max function

Hi,

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.

Thanks,
Sascha
Suresh RaghuramSuresh Raghuram

Hi Sascha,

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_math.htm#apex_System_Math_max_3
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.

Thanks,
Sure.
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?

Thanks,
Sascha
 
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 };
myList.sort();
MaxValue = mylist[4];
MinValue = mylist[0];

Regards,
Bhanu Mahesh