在Groovy jenkins管道脚本中按值对地图进行排序 [英] Sort map by value in Groovy jenkins pipeline script
本文介绍了在Groovy jenkins管道脚本中按值对地图进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Jekins管道脚本中按值对Map
进行自定义排序?
How to do custom sort of Map
for example by value in Jekins pipeline script?
此代码在Jenkins管道脚本中不太起作用:
This code doesn't quite work in Jenkins pipeline script:
Map m =[ james :"silly boy",
janny :"Crazy girl",
jimmy :"funny man",
georges:"massive fella" ]
Map sorted = m.sort { a, b -> a.value <=> b.value }
地图仍未排序.
我决定用一个更好的名称和标签来创建一个单独的问题,因为许多人都在这里努力寻找答案: 时髦定制按值对地图进行排序
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
推荐答案
您将必须为此创建一个带有@NonCPS
批注的单独方法:
You will have to create a separate method with @NonCPS
annotation for that:
@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)
这篇关于在Groovy jenkins管道脚本中按值对地图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文