Max function for more than 2 input parameters

This is a spin-off from this topic. I needed to identify the highest integer in a set within a record.

That would be 431483, 256024 and 431618 in this example. I couldn't figure it out using max() because that takes exactly 2 parameters and I may have more.

So this is what I came up with:

value == row.record.cells['media_%0'].value.sort()[row.record.cells['media_%0'].value.length()-1]

first sort the array default low to high, then select the last item in the array.
Marks the highest in the set true, the other ones false.

One could argue that it's code golfing. I wouldn’t disagree. But it works.

Is there a better way?

Same way I usually do it. One small simplification: to access the last element of an array you can also use the negative indexing -1.

value == row.record.cells['media_%0'].value.sort()[-1]

This makes the expression a bit more readable.

Thanks again, @b2m. This possibilty is not mentioned in the documentation on arrays. Have you come across it elsewhere?

The use of negative indexing is mentioned in the “Syntax” section of the documentation General Refine Expression Language | OpenRefine :

Square brackets can also be used to get substrings and sub-arrays, and single items from arrays:

Example Description
value[1,3] A substring of value, starting from character 1 up to but excluding character 3
"internationalization"[1,-2] Will return “nternationalizati” (negative indexes are counted from the end)
row.columnNames[5] Will return the name of the fifth column

Any function that outputs an array can use square brackets to select only one part of the array to output as a string (remember that the index of the items in an array starts with 0).

For example, partition() would normally output an array of three items: the part before your chosen fragment, the fragment you've identified, and the part after. Selecting only the third part with "internationalization".partition("nation")[2] will output “alization” (and so will [-1], indicating the final item in the array).

I’m sure if some improvement / addition to the documentation could be suggested this could be implemented

1 Like