I was trying to pick a random element from a list of int and the list elements are created with range operator:
def date_range = 1..28
assert ('a'..'d').collect() == ['a','b','c','d'] // So this is instanceof List
assert ('a'..'d') == ['a','b','c','d'] // So this is instanceof List
def x1 = (1..28).collect().sort{new Random()}?.take(1)[0] // works
def x2 = date_range.collect().sort{new Random()}?.take(1)[0] // works
def x3 = date_range[0..27].sort{new Random()}?.take(1)[0] //works
def x4 = date_range.sort{new Random()}?.take(1)[0] // does not works
x4 yielding below Exception
Caught: java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
at listManipulation.run(listManipulation.groovy:21)
What is my mistake on x4?
Update:
My confusion is : if I define date_range as def date_range = 1..28
and check like: assert date_range instanceof List
, than it passes. Why I should again convert it to list ?