Groovy web console

subscribe to the feed Subscribe
to this
site

groupby

Published 1 month ago by ds with tags groupBy
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
class Sample {
    String subjectId
    String visitType
    String barcode

    String toString(){
        "$subjectId $barcode"
    }
}

def samples = [
    new Sample(subjectId: '1001', visitType: 'PN HV', barcode: '123123'),
    new Sample(subjectId: '1001', visitType: 'PN HV', barcode: '123124'),
    new Sample(subjectId: '1002', visitType: 'PN HV', barcode: '123125'),
    new Sample(subjectId: '1002', visitType: 'PN HV', barcode: '123126'),
    new Sample(subjectId: '1002', visitType: 'PN HV', barcode: '123127'),
]

samples
    .groupBy({ s -> s.subjectId })
    .each { 
    println "<h1>${it.key}</h1><p>"
    it.value.each { ss -> 
        println "${ss.barcode}"
    }
    println "</p>"
}