Mahbub Rahman Mahbub Rahman - 2 months ago 13
Groovy Question

Groovy: Iterating a list

I was trying to iterate a list and after googling, I found one solution like below:

value([0,1,2,3,4,5,6].sort{new Random()}?.take(1)[0])


I did not understand this part:
sort{new Random()}

Can someone explain this?
And which class the take method is belonging to?

Answer

sort receives a closure to determine the order that you wish. In this example, it makes a random sort.

take comes from Iterable and returns the first n elements.

Comments