基于多个键对地图列表进行排序

时间:2017-09-26 14:51:05

标签: java arraylist groovy mule dataweave

我有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中实现它。

3 个答案:

答案 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]