You should just be able to iterate through the sorts in reverse precedence:
list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]
list = list.sort{ a,b -> a[2] <=> b[2] }
list = list.sort{ a,b -> a[1] <=> b[1] }
list = list.sort{ a,b -> a[0] <=> b[0] }
assert list == [[1, 0, 3], [1, 5, 2], [2, 0, 1]]
It should establish sorting within the lower precedence and reorder it just enough for upper.
Edit -- If Groovy supports it, a better option would actually be:
list.sort{ a,b -> (a[0] <=> b[0]) || (a[1] <=> b[1]) || (a[2] <=> b[2]) }
In theory, it should return the result of the first comparison that doesn't match. This requires that ||
returns an operand rather than equating true
/false
from them.
Potentially, ?:
might handle it if ||
can't:
list.sort{ a,b -> ((a[0] <=> b[0]) ?: (a[1] <=> b[1])) ?: (a[2] <=> b[2]) }