简体   繁体   English

jenkins groovy 管道排序

[英]jenkins groovy pipeline sorting

it is not possible to correctly sort the sheet.无法正确排序工作表。

script:脚本:

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

result结果

118/ 119/ 120/ 121/ 79/ 80/ ... 118/ 119/ 120/ 121/ 79/ 80/ ...

collestions doesn't help. collestions 没有帮助。 What are the ways to sort the final sheet最终表的排序方式有哪些

You appear to be attempting to sort an array of numeric strings but want them in numeric order.您似乎正在尝试对数字字符串数组进行排序,但希望它们按数字顺序排列。 As strings, they are ordered.作为字符串,它们是有序的。

You want to groovy sort the list, but on numerical value (79 before 121), not string ("1,2,1" before "7,9")您想对列表进行常规排序,但对数值(121 之前的 79),而不是字符串(“7,9”之前的“1,2,1”)进行排序

I'm not 100% clear on what you reference as "the result", but the solution is applicable where appropriate.我不是 100% 清楚您所引用的“结果”,但该解决方案在适当的情况下适用。

If projects is the list,如果项目是列表,

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

If image_id is the list,如果image_id是列表,

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

should return a sorted list.应该返回一个排序列表。

ie: 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

If you are trying to return a sorted map , see this post .如果您尝试返回已排序的地图,请参阅此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM