Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy: How do I sort an ArrayList of String:s in length-of-string order?

How do I sort an ArrayList of String's in length-of-string order in Groovy?

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.

like image 720
knorv Avatar asked Apr 08 '09 08:04

knorv


1 Answers

words = words.sort { it.size() }

To get descending order

words = words.sort { -it.size() }
like image 186
Michael Borgwardt Avatar answered Sep 23 '22 04:09

Michael Borgwardt