Groovy的Map排序 GroovyCC++C#Blog
程序员文章站
2024-02-28 08:24:52
...
//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
- Groovy Map Sort by Value
- Sorting map keys by comparing it's values
- Groovy Maps: Reverse Sort