---
title: "Groovy的Map排序"
author: "rox"
date: "2011-01-12T15:58:01Z"
tags: ["Blog", "C", "C#", "C++", "Groovy"]
categories: ["编程语言"]
slug: "groovy-map"
draft: false
lastmod: "2011-01-12T16:03:42Z"
id: 869343
url: "https://rox.iteye.com/blog/869343"
---

//sort by map values

02.def map = \[a:3, b:2, c:1]

03.map = map.sort {it.value}

04.assert map == \[c:1, b:2, a:3]

05\.  

06.//sort by map values in reverse

07.map = \[a:1, b:2, c:3]

08.map = map.sort {a, b -&gt; b.value &lt;=&gt; a.value}

09.assert map == \[c:3, b:2, a:1]

参考资料：

- JN1035-Maps

[http://groovy.codehaus.org/JN1035-Maps](http://groovy.codehaus.org/JN1035-Maps)

- Groovy Map Sort by Value

[http://groovyconsole.appspot.com/script/29001](http://groovyconsole.appspot.com/script/29001)

- Sorting map keys by comparing it's values

[http://snipplr.com/view/2789/sorting-map-keys-by-comparing-its-values/](http://snipplr.com/view/2789/sorting-map-keys-by-comparing-its-values)

- Groovy Maps: Reverse Sort

[http://www.intelligrape.com/blog/2009/04/15/groovy-maps-reverse-sort/](http://www.intelligrape.com/blog/2009/04/15/groovy-maps-reverse-sort/)