How to sort alphanumeric string in groovy

118 Views Asked by At

How to sort alphanumeric values using groovy ?For example : I have list of values [ADF1SD23GF12,UTRR453FGT3,NKUY43ERT5,56GHY123,MU157FGR234,...] I want to get result as in Excel sorting for this values. Could someone help?

List1.sort{ a,b -> a <=> b }

1

There are 1 best solutions below

0
chubbsondubs On

There are probably some differences between what you're after and what the close to the default sorting does, but you can modify this example to get there:

def arr = ["one", "two", "three", "four"]

arr.sort() { a, b -> a.compareTo( b ) }
println( arr )

arr.sort() { a, b -> a.compareTo( b ) * -1 }
println( arr )

Prints:

[four, one, three, two]
[two, three, one, four]

Of course you can simply the first one to simply:

arr.sort()
println( arr )

But I left it with the closure to illustrate how to sort ascending vs descending.