Jenkins groovy管道排序



不可能对工作表进行正确排序。

脚本:

import groovy.json.JsonSlurper
if (TAGS != 'all') {
return []
}
def image_id = []
def projectList = new URL("https://gitlab.ru/api/v4/search?scope=projects&search=$PROJECT&private_token=$GITLAB_JENKINS_TOKEN")
def projects = new groovy.json.JsonSlurper().parse(projectList.newReader())
projects.each {
project_id = it.id
}
def repository_name = "$NAME_REPOSITORY"
def id = repository_name.tokenize('/ ')[-1].tokenize('.')[0]
def repository_id = id
def imageList = new URL("https://gitlab.ru/api/v4/projects/$project_id/registry/repositories/$repository_id/tags?per_page=100&private_token=$GITLAB_JENKINS_TOKEN")
def image = new groovy.json.JsonSlurper().parse(imageList.newReader())
image.each {
image_id.add(it.name)
}

return image_id
结果

118/119/120/121/79/80/…

collections没有帮助。有哪些方法对最终工作表进行排序

您似乎正在尝试对数字字符串数组进行排序,但希望它们按数字顺序排列。作为字符串,它们是有序的。

您想对列表进行groovy排序,但要对数值(79在121之前)进行排序,而不是字符串("1,2,1")在"7、9")

我不是100%清楚你所说的"结果"是什么,但解决方案是适用的。

如果

>项目

是列表,
projects.sort(){ a, b -> a.id.toInteger() <=> b.id.toInteger() }.each {
// process numerically sorted items
}

如果image_id是列表,

return image_id.sort(){ a, b -> a.toInteger() <=> b.toInteger() }

应该返回一个排序过的列表。

ie:

def list = ['118', '119', '120', '78', '79']
println "raw sort"
list.sort().each {
println it
}
println "Integer sort"
list.sort(){ a, b -> a.toInteger() <=> b.toInteger() }.each {
println it
}
return
raw sort
118
119
120
78
79
Integer sort
78
79
118
119
120

如果您试图返回排序映射,请看这篇文章。

最新更新