groovy - Sorting a resultset during the iteration over them -


given xml file following content

<root>   <group name="database">       <user name="thorsten"/>      <user name="karl"/>      <user name="beate"/>      <user name="heinz"/>      <user name="andreas"/>   </group> </root> 

now can read groovy script this

def out = ""; def result = new xmlslurper().parse(new file("d:\\user.xml")); result.group.user.each { out += it.@name.text()+ '\n'; } println out; 

the output in order appear in xml file

thorsten karl beate heinz andreas 

is possible sort, in alphabetic order, resultset during iteration on them?

georg

during iteration? of course not.

but if use xmlparser, this:

def doc = new xmlparser().parse(new file("users.xml")); foo = doc.group.user.sort { it.@name }.collect { it.@name }.join("\n") println foo 

which outputs:

andreas beate heinz karl thorsten 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -