try the following:
def test=["Group 1", "Group2", "Group3", "2", "Group20", "Group30", "1", "Grape 1", "Grape 12", "Grape 2", "Grape 22"] test.sort{ a,b -> def n1 = (a =~ /\d+/)[-1] as Integer def n2 = (b =~ /\d+/)[-1] as Integer def s1 = a.replaceAll(/\d+$/, '').trim() def s2 = b.replaceAll(/\d+$/, '').trim() if (s1 == s2){ return n1 <=> n2 } else{ return s1 <=> s2 } } println test
If you want to first compare the number you must change if:
if (n1 == n2){ return s1 <=> s2 } else{ return n1 <=> n2 }
This will take the last number found in the string, so you can write whatever you want, but the βindexβ should be the last number
source share