jastrachan | 29 Nov 13:14 2004
Picon

Re: Collection sorting

It can take a closure of 1 argument, in which case the closure acts as 
a transformer to the thing which should be compared, or 2 arguments, in 
which case the closure acts like a regular comparator.

people.sort { it.name }

or

people.sort { a, b | return a.name <=> b.name }

On 29 Nov 2004, at 12:09, Babelex wrote:
> Hi all
>
> Just browsing through the groovy reference summary i notice the
> collection.sort(){..} closure support. Obviously on the sheet it
> doesnt actually show one how it operates except to indicate the
> closure acts as a comparator, I just need to work out how to use this
> can anyone give me an example, i was thinking this kind of thing :
>
> myCollectionOfNumbers.sort() {key | return it < key ? -1 : (it == key 
> ? 0 : 1)}
>
> to sort a collection of integers
>
> I may be well of base here not being a groovy expert, my actual
> problem is also more complex it's a collection of stings that i wish
> to arrange alphabetically or numerically (even though some arent real
> numbers)
>
> Anyone care to touch me with a clue stick ..
> -- 
> regards
> babelex
>
>
> #OpenSource means free to innovate#
>
>

James
-------
http://radio.weblogs.com/0112098/


Gmane