Make it static, i.e. append "static" before "def" On Mon, May 16, 2016 at 6:57 PM Guy Matz <guym...@gmail.com> wrote:
> Thanks! Not working, though. Getting: > No such property: compareVersions for class: Sorters > > with: > > class Sorters { > def compareVersions = { afile, bfile -> > return getVersion(afile).toInteger() <=> getVersion(bfile).toInteger() > } > } > > and called with: > > res.sort(Sorters.compareVersions) > > > Any thoughts are appreciated! Thanks!! > > > > On Mon, May 16, 2016 at 11:43 AM, Søren Berg Glasius (GR8Conf EU) < > sbglas...@gr8conf.org> wrote: > >> 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 <guym...@gmail.com> <guym...@gmail.com> >> Reply: users@groovy.apache.org <users@groovy.apache.org> >> <users@groovy.apache.org> >> Date: May 16, 2016 at 17:42:21 >> To: users@groovy.apache.org <users@groovy.apache.org> >> <users@groovy.apache.org> >> 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) < >> sbglas...@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 <guym...@gmail.com> <guym...@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 >>> >>> >> >