I’m a beginner at groovy and I can’t seem to understand this code. Can you please tell me how does this code operate?
def list = [ [1,0], [0,1,2] ]
list = list.sort { a,b -> a[0] <=> b[0] }
assert list == [ [0,1,2], [1,0] ]
what I know is the second line should return the value of 1 because of the spaceship operator but what is the use of that? and what type of sort is this? (there are 6 sort methods in the gdk api and i’m not really sure which is one is used here)
The
sortin your code snippet uses the comparator argument method call – see http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort(java.util.Comparator)So, you are sorting the collection using your own comparator. Now the comparator simply uses the first element of the inner collection to decide the order of the outer collection.