简体   繁体   中英

Sort map by value in Groovy jenkins pipeline script

How to do custom sort of Map for example by value in Jekins pipeline script?

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 }

The map is still not sorted.

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

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)

params name

  • 1.xx
  • 2.xx
  • ...
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/ }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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