user8903285 user8903285 - 1 year ago 115
Groovy Question

equivalent sortby in groovy (ruby)?

Which is the equivalent of sortby in groovy java ?
Which is the equivalent of sortby in groovy java ?

For example for doing this

.sort_by{|e| e.split(";").count }


please help me out.

import groovyx.gpars.*
class Test{
public static void main(String[] args){
def o=0
println 'n,n,n,n,n,n,n,n'.replaceAll(/n/) { ++o }
String fileContents = new File('/home/ffff /tddddc.txtd').getText('UTF-8').split(';').sort { e1, e2 -> e1.split('+').length <=> e2.split('+').length }.join(';')
println fileContents
}
}


Thank you.

Answer Source

Groovy and Java are different languages. For groovy, see this question: groovy sort with comparator syntax

Consider this groovysh (Groovy shell) session:

groovy:000> x = ["a;b;c","d;e", "f;g;h;i", "j"]
===> [a;b;c, d;e, f;g;h;i, j]
groovy:000> x.sort{x,y -> x.split(";").length <=> y.split(";").length}
===> [j, d;e, a;b;c, f;g;h;i]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download