2016年12月6日 星期二

[ 常見問題 ] Sort list in descending order - groovy madness

Source From Here 
Question 
Consider I have a list as below: 
groovy:000> list = ['ab', 'a', 'abcd', 'abc']
===> [ab, a, abcd, abc]

How do I sort the list in descending order? 

How-To 
The Groovy list provide API sort for you to provide lambda on how to do sorting: 
groovy:000> list.sort{a,b-> a.size() <=> b.size()} // Sorting with length of string in ascending order
===> [a, ab, abc, abcd]
groovy:000> list // The sorting is in place
===> [a, ab, abc, abcd]
groovy:000> list.reverse() // To get a descending order
===> [abcd, abc, ab, a]
groovy:000> list // The reverse() will return new List which means it is not in place
===> [a, ab, abc, abcd] 

Supplement 
[ In Action ] The collective Groovy datatypes - Working with lists

沒有留言:

張貼留言

[Linux 常見問題] sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix

Source From  Here   Preface   I‘m trying to run the following command:  #   ssh  user@box.example.com  sudo command1 /path/to/file But, a...