简体   繁体   English

Groovy排序字符串升序

[英]Groovy sorting string asc

How to sort string names in array on ascending order . 如何按升序对数组中的字符串名称进行排序。

I tried sort method but it fails to sort on name basis . 我尝试了排序方法,但是它无法基于名称进行排序。

def words = ["orange", "blue", "apple", "violet", "green"] def字= [“橙色”,“蓝色”,“苹果”,“紫”,“绿色”]

I need to achieve like this : 我需要实现这样的目标:

["apple", "blue", "green", "orange", "violet" ] [“苹果”,“蓝色”,“绿色”,“橙色”,“紫”]

thanks in advance. 提前致谢。

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

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

You can also change the indexes based on the requirement 您也可以根据需要更改索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM