Groovy排序字符串asc

时间:2010-03-30 07:01:23

标签: groovy sorting

如何按升序对数组中的字符串名称进行排序。

我尝试了排序方法,但无法按名称排序。

def words = [“orange”,“blue”,“apple”,“violet”,“green”]

我需要达到这样的目的:

[“apple”,“blue”,“green”,“orange”,“violet”]

提前谢谢。

2 个答案:

答案 0 :(得分:4)

["orange", "blue", "apple", "violet", "green"].sort()

答案 1 :(得分:1)

def words = ["orange", "blue", "apple", "violet", "green"]

["orange", "blue", "apple", "violet", "green"].sort({ a, b -> a[0] <=> b[0] } as Comparator )

您还可以根据要求更改索引