我最近在学习使用Groovy处理数组。我想知道如何对一个字符串数组按字母表顺序排序。目前我的代码从用户处获取字符串输入并按照顺序和相反顺序打印出来:
System.in.withReader {
def country = []
print 'Enter your ten favorite countries:'
for (i in 0..9)
country << it.readLine()
print 'Your ten favorite countries in order/n'
println country //prints the array out in the order in which it was entered
print 'Your ten favorite countries in reverse'
country.reverseEach { println it } //reverses the order of the array
我该怎样按字母顺序打印它们?
country.sort(java.text.Collator.instance)
。 - ataylor.sort().reverse()
或自定义闭包一步完成反向排序。 - doelleri