how sort
works on list of lists when spaceship
operator (compareTo) fails for list?
Note that it's not just equals
, but compareTo
(greater ,lower, or equal)
def v1=[1, 2, 0]
def v2=[1, 3, 0]
def v=[v2,v1]
println v
//prints> [[1, 3, 0], [1, 2, 0]]
v=v.sort()
assert v[0]==v1
println v
//prints> [[1, 2, 0], [1, 3, 0]]
c=c.sort{x,y-> y<=>x}
//throws Cannot compare java.util.ArrayList with value '[1, 3]' and java.util.ArrayList with value '[1, 2, 3]'
According to code above List<List>.sort()
works.
According to Iterable.sort() documentation:
Sorts the Collection. Assumes that the collection items are comparable and uses their natural ordering to determine the resulting order.
The question why and how List<List>.sort()
works in groovy?
Can somebody point me how comparison of arrays implemented in groovy for sort()
operation?