How can I compare two arrays contains same items or not in groovy?
Simply sorting the results and comparing is an easy way, if your lists are not too large:
def a = [1, 3, 2]def b = [2, 1, 3]def c = [2, 4, 3, 1]def haveSameContent(a1, a2) { a1.sort(false) == a2.sort(false)}assert haveSameContent(a, b) == trueassert haveSameContent(a, c) == false
The false
passed to sort
is to prevent in-place reordering. If it's OK to change the order of the lists, you can remove it and possibly gain a little bit of performance.