Home > OS >  Sort Map of Maps based on one of the nested values
Sort Map of Maps based on one of the nested values

Time:10-26

def map = [
  P1: [name:"Jerry", age: 42, city: "New York"],
  P2: [name:"Long", age: 25, city: "New York"],
  P3: [name:"Dustin", age: 29, city: "New York"],
  P4: [name:"Dustin", age: 34, city: "New York"]];

I have the above map, I want to sort the map based on the age

CodePudding user response:

You could do something like this:

def map = [
  P1: [name:"Jerry", age: 42, city: "New York"],
  P2: [name:"Long", age: 25, city: "New York"],
  P3: [name:"Dustin", age: 29, city: "New York"],
  P4: [name:"Dustin", age: 34, city: "New York"]]
  
def sortedByAge = map.sort { entry ->
    entry.value.age
}
  • Related