ITeye博客

Groovy的Map排序

rox · 2011-01-12 15:58 · 编程语言 · C · C++ · Groovy · Blog · C# · Markdown

//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 -> b.value <=> a.value}

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

参考资料:

  • JN1035-Maps

http://groovy.codehaus.org/JN1035-Maps

  • Groovy Map Sort by Value

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/

  • Groovy Maps: Reverse Sort

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。