{"id":43,"date":"2009-04-15T18:57:45","date_gmt":"2009-04-15T13:27:45","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=43"},"modified":"2016-12-19T15:28:10","modified_gmt":"2016-12-19T09:58:10","slug":"groovy-maps-reverse-sort","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-maps-reverse-sort\/","title":{"rendered":"Groovy Maps: Reverse Sort"},"content":{"rendered":"<p>I want to quickly share how you can sort a Map by its key\/value\/field of valueObject; in either ascending or descending order using Groovy<\/p>\n<p>I created a <code>Map&lt;String, Person&gt;<\/code> as follows:<\/p>\n<pre lang=\"groovy\">\r\nclass Person {\r\n    String name\r\n    Date dateOfBirth\r\n\r\n    static constraints = {\r\n    }\r\n}\r\n\r\nMap <String, Person> people = [:]\r\n[*(11..20), *(1..10)].each {\r\n    Person person = new Person()\r\n    person.name = \"Person-\" + it\r\n    use(org.codehaus.groovy.runtime.TimeCategory) {\r\n        person.dateOfBirth = new Date() - (new Random().nextInt(15) + 10).years\r\n    }\r\n    people[person.name] = person\r\n}\r\n<\/pre>\n<p>Now, Sorting maps in ascending order is a simple one line code:<\/p>\n<pre lang=\"groovy\">\r\n\/\/ Sort by Key(Person's Name):\r\nmap = map.sort {it.key}\r\n\r\n\/\/ Sort by Value(Person Object):\r\nmap = map.sort {it.value}\r\n\r\n\/\/ Sort by a particular field of Value(Date Of Birth):\r\nmap = map.sort {it.value.dateOfBirth}<\/pre>\n<p>But how about sorting in reverse order. It would have been great to have map.reverse() or map.reverseSort{} available in Groovy. Since none exists, I had to create the following method:<\/p>\n<pre lang=\"groovy\">\r\nMap reverseSortMap(Map unsortedMap) {\r\n\r\n    \/\/ Different implementations of Comparator\r\n    \/\/ 1. Reverse Sort by Key(Person's Name):\r\n    Comparator comparator = [compare: {a , b ->\r\n    b.compareTo(a)\r\n    }] as Comparator\r\n\r\n    \/\/ 2. Reverse Sort by Value(Person Object):\r\n    Comparator comparator = [compare: {a , b ->\r\n    unsortedMap.get(b).compareTo(unsortedMap.get(a))\r\n    }] as Comparator\r\n\r\n    \/\/ 3. Reverse Sort by a particular field of Value(Date Of Birth):\r\n    Comparator comparator = [compare: {a , b ->\r\n    unsortedMap.get(b).dateOfBirth.compareTo(unsortedMap.get(a).dateOfBirth)\r\n    }] as Comparator\r\n\r\n    Map sortedMap = new TreeMap(comparator)\r\n    sortedMap.putAll(unsortedMap)\r\n\r\n    return sortedMap\r\n}<\/pre>\n<p>Use either of the Comparator implementations above &amp; use the method:<\/p>\n<pre lang=\"groovy\">\r\npeople = reverseSortMap(people)<\/pre>\n<p>Hope it helps.<\/p>\n<p>\u2013<br \/>\n~Aman Aggarwal<br \/>\naman@intelligrape.com<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/\" onclick=\"javascript:pageTracker._trackPageview(&#039;\/outbound\/article\/www.IntelliGrape.com&#039;);\">http:\/\/www.IntelliGrape.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to quickly share how you can sort a Map by its key\/value\/field of valueObject; in either ascending or descending order using Groovy I created a Map&lt;String, Person&gt; as follows: class Person { String name Date dateOfBirth static constraints = { } } Map people = [:] [*(11..20), *(1..10)].each { Person person = new [&hellip;]<\/p>\n","protected":false},"author":282,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":89},"categories":[7],"tags":[9,67,68,69],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/282"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}