Sorting a Map by value with Groovy is very simple, due to the added ‘sort’ method on the Map interface.
def hashMap = "fields": {
"field_3": {
"order": 4
},
"field_2": {
"order": 3
},
"field_1": {
"order": 2
},
"field_0": {
"order": 6
}
}
hashMap.sort { a , b -> a.value.order <=> b.value.order }
def hashMap = "fields": {
"field_3": {
"order": 4
},
"field_2": {
"order": 3
},
"field_1": {
"order": 2
},
"field_0": {
"order": 6
}
}
hashMap.sort { a , b -> a.value.order <=> b.value.order }
No comments:
Post a Comment