简体   繁体   中英

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"]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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