Hi dear,
I have written code like in the picture, I just want to print the max no without using max function.
Discussion posts and replies are publicly visible
You can create a local variable of Map type and use to data subset to sort the array descending and get the 1st index value
a!localVariables( local!listOfInts: { a!map(num: 5), a!map(num: 6), a!map(num: 2), a!map(num: 1), a!map(num: 10), a!map(num: 7) }, index( index( todatasubset( local!listOfInts, a!pagingInfo(1, - 1, a!sortInfo("num", false())) ).data, "num", {} ), 1, {} ) )
Can't we achieve this by looping?
where you are looking for looping and why?
You could do this with looping, but max() is by far the easiest and fastest method. I would also be interested in the use case reasoning to avoid max().
deepakg's example with todatasubset() sorting is a commonly used solution for similar items as well.
Additionally, max value from nested a!forEach():
a!localVariables( local!list: {1,3,8,6,5,2}, index( reject( fn!isnull, a!forEach( items: local!list, expression: { a!localVariables( local!item: fv!item, if( count( wherecontains( true, a!forEach( items: local!list, expression: toboolean(fv!item<local!item) ) ) )=count(local!list)-1, fv!item, null ) ) } ) ), 1, null ) )
Max with sortintegerarray():
index( sortintegerarray({1,3,8,6,5},true), 1, null )
Hi shukurs0001,
I want to understand your use case. Why do you want to do it without the max() function? Tho there are multiple ways of doing it, max() will be the most efficient way (as Chris also mentioned)
max() is fairly useless in, let's say, the case of having an array of CDT and wanting to find the element in the array with the max value on a particular element - in which case, the "todatasubset" sorting method listed above works pretty well overall.
Using max() with displayValue()?
I love displayValue().
a!localVariables( local!data: { 'type!{urn:com:gdit:types}COE_Sample_CDT'(step: 2, requestByName: "Mike"), 'type!{urn:com:gdit:types}COE_Sample_CDT'(step: 3, requestByName: "Harshit"), 'type!{urn:com:gdit:types}COE_Sample_CDT'(step: 1, requestByName: "Chris"), 'type!{urn:com:gdit:types}COE_Sample_CDT'(step: 4, requestByName: "Stefan") }, displayValue( max(local!data.step), local!data.step, local!data.requestByName, null ) )
That works too, though at that point you may as well sort and get the 1st index (especially since setting it up that way from the start enables you to potentially add other sort factors, like if one is needed to break a tie). Also I find displayValue() to be unnecessarily messy-looking in most cases, though it is handy in a few.