Groovy中映射的排序值
我有一个映射,其中一个键包含多个值Groovy中映射的排序值,groovy,Groovy,我有一个映射,其中一个键包含多个值 datamap = [ 'Antenna Software':[ 'Salarpuria', 'Cessna', 'Vrindavan Tech', 'Alpha Center' ], 'Ellucian':[ 'Malvern', 'Ellucian House', 'Residency Road'] ] 这里我需要按字母顺序对值进行排序 datamap = [ 'Antenna Software':[ 'Alpha Cente
datamap = [ 'Antenna Software':[ 'Salarpuria', 'Cessna', 'Vrindavan Tech', 'Alpha Center' ],
'Ellucian':[ 'Malvern', 'Ellucian House', 'Residency Road'] ]
这里我需要按字母顺序对值进行排序
datamap = [ 'Antenna Software':[ 'Alpha Center', 'Cessna', 'Salarpuria', 'Vrindavan Tech' ],
'Ellucian':[ 'Ellucian House', 'Malvern', 'Residency Road' ] ]
如何以groovy的方式执行此操作?您应该能够执行以下操作:
def sortedMap = datamap.sort().collectEntries { k, v ->
[ k, v.sort( false ) ]
}
如果您不介意对映射的键进行排序,那么可以去掉初始的sort()
:
排序说明(错误)
:
,因此:
因此,它只保留原始列表,只返回排序后的列表:
// Given a List
def a = [ 3, 1, 2 ]
// We can sort it (passing false)
def b = a.sort( false )
// And the result is sorted
assert b == [ 1, 2, 3 ]
// AND the original list has remained the same
assert a == [ 3, 1, 2 ]
为什么是v.sort(假)??“false”表示什么?def dataMap=[:]Client.list(sort:“name”,order:“desc”)。每个{Client->dataMap[Client]=Client.victions}def sortedMap=dataMap.collectEntries{k,v->[k,v.sort(false)]以什么方式失败?只是说“这失败了”没有多大帮助。。。Try:
def sortedMap=Client.list(sort:'name',order:'desc').collectEntries{Client->[Client.name,Client.victions.name.sort()}
(假设场馆有一个name
属性)客户端有一个场馆客户端列表。ventions.name.sort()没有work@monda“不起作用”并不比“失败”好多少请用您的实际问题和收到的任何错误消息更新您的问题
// Given a List
def a = [ 3, 1, 2 ]
// We can sort it
def b = a.sort()
// And the result is sorted
assert b == [ 1, 2, 3 ]
// BUT the original list has changed too!
assert a != [ 3, 1, 2 ] && a == [ 1, 2, 3 ]
// Given a List
def a = [ 3, 1, 2 ]
// We can sort it (passing false)
def b = a.sort( false )
// And the result is sorted
assert b == [ 1, 2, 3 ]
// AND the original list has remained the same
assert a == [ 3, 1, 2 ]