krish
krish

Reputation: 13

Sort Map of Maps based on one of the nested values

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

Upvotes: 0

Views: 82

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27245

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
}

Upvotes: 1

Related Questions