[英]Sort map by value in Groovy jenkins pipeline script
How to do custom sort of Map
for example by value in Jekins pipeline script?如何在 Jekins 管道脚本中按值对
Map
进行自定义排序?
This code doesn't quite work in Jenkins pipeline script:此代码在 Jenkins 管道脚本中不太适用:
Map m =[ james :"silly boy",
janny :"Crazy girl",
jimmy :"funny man",
georges:"massive fella" ]
Map sorted = m.sort { a, b -> a.value <=> b.value }
The map is still not sorted. map 仍未排序。
I decided to crate a separate question with better name and tags, because many people were struggling to find an answer here: Groovy custom sort a map by value我决定用更好的名称和标签创建一个单独的问题,因为很多人都在努力在这里找到答案: Groovy custom sort a map by value
You will have to create a separate method with @NonCPS
annotation for that:为此,您必须使用
@NonCPS
注释创建一个单独的方法:
@NonCPS
def getSorted(def toBeSorted){
toBeSorted.sort(){ a, b -> b.value <=> a.value }
}
And then call it from the pipeline script.然后从管道脚本中调用它。
Map unsortedMap =[ james :"silly boy",
janny :"Crazy girl",
jimmy :"funny man",
georges:"massive fella" ]
def sortedMap = getSorted(unsortedMap)
pipeline {
agent {
kubernetes {
inheritFrom 'seunggabi-batch'
defaultContainer 'seunggabi-batch'
}
}
environment {
COUNTRY = "kr"
ENV = "prod"
CLASS = "seunggabi.batch.job.SparkSubmitJob"
}
stages {
stage('Run Job') {
steps {
script {
ARGS = sorted(params).collect { /$it.value/ } join ","
}
sh "/app/static/sh/emr.sh 1 20 ${COUNTRY} ${ENV} ${CLASS} \"${ARGS}\""
}
}
}
}
@NonCPS
def sorted(def m){
m.sort { /$it.key/ }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.