Computing the Minimum of Items in a Collection

To determine the minimum item in a collection call its min() function with no arguments.

However, if you need to find the minimum from a subset of items in a collection based on a particular condition, then pass a closure to min() that identifies the expression for which to find the minimum value. For example, to find the minimum item in the following list of users based on the number of accesses they've made to a system, do the following:

def users = [
             'smuench':[name:'Steve', badge:'A123', accesses: 135],
             'sburns':[name:'Steve', badge:'C789', accesses: 52],
             'qbronson':[name:'Quello', badge:'Z231', accesses: 52],
             'jevans':[name:'Joe', badge:'B456', accesses: 1001]
            ]
// Return the map entry with the minimum value based on accesses
def minUser = users.min { it.value.accesses }
The min() function returns the first item having the minimum accesses value of 52, which is the map entry corresponding to sburns. However, to return all users having the minimum value requires first determining the minimum value of accesses and then finding all map entries having that value for their accesses property. This code looks like:
// Find the minimum value of the accesses property
def minAccesses = users.min { it.value.accesses }.value.accesses
// Return all map entries having that value for accesses
def usersWithMinAccesses = users.findAll{ it.value.accesses == minAccesses }

There is often more than one way to solve a problem. Another way to compute the minimum number of accesses would be to first collect() all the accesses values, then call min() on that collection of numbers. That alternative approach looks like this:

// Find the minimum value of the accesses property
def minAccesses = users.collect{ it.value.accesses }.min()
Using either approach to find the minimum accesses value, the resulting map produced is:
[
 sburns:[name:'Steve', badge:'C789', accesses:52],
 qbronson:[name:'Quello', badge:'Z231', accesses:52]
]

If the collection whose minimum item you seek requires a custom comparison to be done correctly, then you can pass the same kind of two-parameter comparator closure that the sort() function supports.