我有List
Map
个,如下所示:
[{type=UNL,date=20170606,publication=ECC,boNumber=34,value=500}
{type=UNP,date=20190807,publication=ECD,boNumber=24,value=100}
{type=UNA,date=20170606,publication=ECC,boNumber=34,value=400}
{type=UNC,date=20170606,publication=ECC,boNumber=34,value=300}
{type=UNF,date=20190807,publication=ECD,boNumber=24,value=200}]
对我来说,最重要的字段是date,publication,boNumber
的值,例如20170606 ECC 34
。我想使用这三个字段对此List
进行排序。
预期结果
[{type=UNL,date=20170606,publication=ECC,boNumber=34,value=500}
{type=UNA,date=20170606,publication=ECC,boNumber=34,value=400}
{type=UNC,date=20170606,publication=ECC,boNumber=34,value=300}
{type=UNP,date=20190807,publication=ECD,boNumber=24,value=100}
{type=UNF,date=20190807,publication=ECD,boNumber=24,value=200}]
如何使用groovy,dataweave或Java在mule中实现它。
答案 0 :(得分:3)
你所追求的是.sort { [it.date, it.publication, it.boNumber] }
。
def listOfMaps = [[type:'UNL',date:20170606,publication:'ECC',boNumber:34,value:500],
[type:'UNP',date:20190807,publication:'ECD',boNumber:24,value:100],
[type:'UNA',date:20170606,publication:'ECC',boNumber:34,value:400],
[type:'UNC',date:20170606,publication:'ECC',boNumber:34,value:300],
[type:'UNF',date:20190807,publication:'ECD',boNumber:24,value:200]]
listOfMaps
.each { println it }
println()
listOfMaps
.sort { [it.date, it.publication, it.boNumber] }
.each { println it }
例如输出
[type:UNL, date:20170606, publication:ECC, boNumber:34, value:500]
[type:UNP, date:20190807, publication:ECD, boNumber:24, value:100]
[type:UNA, date:20170606, publication:ECC, boNumber:34, value:400]
[type:UNC, date:20170606, publication:ECC, boNumber:34, value:300]
[type:UNF, date:20190807, publication:ECD, boNumber:24, value:200]
[type:UNL, date:20170606, publication:ECC, boNumber:34, value:500]
[type:UNA, date:20170606, publication:ECC, boNumber:34, value:400]
[type:UNC, date:20170606, publication:ECC, boNumber:34, value:300]
[type:UNP, date:20190807, publication:ECD, boNumber:24, value:100]
[type:UNF, date:20190807, publication:ECD, boNumber:24, value:200]
答案 1 :(得分:0)
您可以使用Dataweave实现此目的
%dw 1.0
%output application/json
---
payload orderBy ($.date ++ $.publication ++ $.boNumber)
希望这有帮助。
答案 2 :(得分:0)
Groovy的解决方案如下所示:
List list = [[type: 'UNL', date: '20170606', publication: 'ECC', boNumber: 34, value: 500],
[type: 'UNP', date: '20190807', publication: 'ECD', boNumber: 24, value: 100],
[type: 'UNA', date: '20170606', publication: 'ECC', boNumber: 34, value: 400],
[type: 'UNC', date: '20170606', publication: 'ECC', boNumber: 34, value: 300],
[type: 'UNF', date: '20190807', publication: 'ECD', boNumber: 24, value: 200]]
List sorted = list.sort { a,b ->
a.date == b.date ?
(a.publication == b.publication ?
a.boNumber <=> b.boNumber :
a.publication <=> b.publication) :
a.date <=> b.date
}
sorted.each { println it }
传递给.sort()
方法的Closure确实如下:
date
字段publication
字段是否相等publication
字段相等,则按boNumber
publication
[type:UNL, date:20170606, publication:ECC, boNumber:34, value:500]
[type:UNA, date:20170606, publication:ECC, boNumber:34, value:400]
[type:UNC, date:20170606, publication:ECC, boNumber:34, value:300]
[type:UNP, date:20190807, publication:ECD, boNumber:24, value:100]
[type:UNF, date:20190807, publication:ECD, boNumber:24, value:200]