Hi
You can define it in a class as a static closure
class Sorters {
static compareVersions = { a,b ->
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
}
and use it like:
list.sort(Sorters.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 <[email protected]>
Reply: [email protected] <[email protected]>
Date: May 16, 2016 at 17:42:21
To: [email protected] <[email protected]>
Subject: Re: re-using a comparison closure
Thanks! Now, I have a number of methods that need access to that closure . . .
Can I make the closure global? Is there a better way?
Thanks again,
Guy
On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU)
<[email protected]> wrote:
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 <[email protected]>
Reply: [email protected] <[email protected]>
Date: May 16, 2016 at 17:28:34
To: [email protected] <[email protected]>
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