How do I sort an ArrayList
of String
's in length-of-string order in Groovy?
如何在Groovy中按字符串长度顺序对String的ArrayList进行排序?
Code:
码:
def words = ['groovy', 'is', 'cool']
// your code goes here:
// code that sorts words in ascending length-of-word order
assert words == ['is', 'cool', 'groovy']
There are certainly more than one way to do it - so I'll grant the answer to the person who provides the most elegant solution.
肯定有不止一种方法可以做到 - 所以我会给那些提供最优雅解决方案的人一个答案。
1 个解决方案
#1
29
words = words.sort { it.size() }
To get descending order
要降序
words = words.sort { -it.size() }
#1
29
words = words.sort { it.size() }
To get descending order
要降序
words = words.sort { -it.size() }