groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Guy Matz <guym...@gmail.com>
Subject Re: re-using a comparison closure
Date Mon, 16 May 2016 15:42:09 GMT
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) <
sbglasius@gr8conf.org> 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 <guymatz@gmail.com> <guymatz@gmail.com>
> Reply: users@groovy.apache.org <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>
> <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