In this blog we will know how to sort map by it's value.
Here are the steps to sort map by value in ascending or descending order
Step-1. Let we have a map .
Now if we need to sort it by it's value in ascending order
def map = [apple:3, banana:2, mongo:1] // ascending order map = map.sort {it.value} println map // output [mongo:1, banana:2, apple:3]
Now if we need to sort it by it's value in ascending order
// descending order map = map.sort {a, b -> b.value <=> a.value} println map // output [apple:3, banana:2, mongo:1] >
THANKS
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pankaj Kumar Yadav
Pankaj has been working as a Grails developer expertise in struts, spring, ejb, hibernate and angularjs framework.