collections - How to make Groovy arrays comparable? -
tried this:
arraylist.metaclass.compareto = {arg -> this?.size() <=> arg?.size() } [1]<=>[2]
it doesn't work.
there still exception rises groovy.lang.groovyruntimeexception: cannot compare java.util.arraylist value '[1]' , java.util.arraylist value '[2]'
one approach implement comparator
interface.
another use metaclass
wanted, not able use <=>
operator since list
doesn't implement comparable
.
list.metaclass.compareto = { collection other -> delegate.size() <=> other?.size() } def x = [1, 2, 3] def y = [4, 5] println x.compareto(y) // x <=> y won't work
Comments
Post a Comment