Reputation: 113
I have an array like this:
def array = [
"release-3.0.0-1-a6gbd6",
"release-3.0.0-10-h7bdbc",
"release-3.0.0-12-7hbs6",
"release-3.0.0-23-9sz6gd",
"release-3.0.0-3-g6h8xd",
]
I need it to be able to sort it by numbers in the middle (e.g. 3.0.0-1) in reverse order to look like this:
def array = [
"release-3.0.0-23-9sz6gd",
"release-3.0.0-12-7hbs6",
"release-3.0.0-10-h7bdbc",
"release-3.0.0-3-g6h8xd",
"release-3.0.0-1-a6gbd6",
]
How can I do this with Groovy?
I tried following Groovy: How to sort String array of text+numbers by last digit but am not having any luck
Upvotes: 0
Views: 1215
Reputation: 113
This appears to be working
def newArray = array.sort(false){[it.tokenize('-')[-3], it.tokenize('-')[-2] as Integer]}
Here we are sorting on the string after the first dash, and then sorting by the integer after the second dash
Upvotes: 1