groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Søren Berg Glasius (GR8Conf EU)" <sbglas...@gr8conf.org>
Subject Re: re-using a comparison closure
Date Mon, 16 May 2016 15:30:54 GMT
Hi Guy

Just assign the variable 

def comapreVersions = { a,b ->
	return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}

and then use it in your sort:


list.sort(compareVersions)



Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Guy Matz <guymatz@gmail.com>
Reply: users@groovy.apache.org <users@groovy.apache.org>
Date: May 16, 2016 at 17:28:34
To: users@groovy.apache.org <users@groovy.apache.org>
Subject:  re-using a comparison closure  

Hi!
I have to sort a list of strings based on a number within the string . . .  I am able to
sort using something like:
list.sort( { a,b -> getVersion(a) <=> getVersion(b)})

I need to use this in a bunch of places in my code and was hoping to replace it with a method,
like:
list.sort( compareVersions)

with compareVersions:
def compareVersions(a, b) {
  return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}

putting the method (compoareVersions) into the sort as a param doesn't work.  Anyone know
what I'm missing?

Thanks!!
Guy
Mime
View raw message