logo       

Re: Collection sorting: msg#00188

lang.groovy.user

Subject: Re: Collection sorting

Excellent thanks James

I had to use : sortedKeys = keys.sort(){a,b | return a <=> b}
if i just used keys.sort(){a,b | return a <=> b} and then iterated
through keys it wasnt sorted (keys is the result of a map.keySet() ) ,
which indicates it doesnt sort the original collection but returns a
collection of sorted objects , is that correct?


On Mon, 29 Nov 2004 12:14:05 +0000, jastrachan-ee4meeAH724@xxxxxxxxxxxxxxxx
<jastrachan-ee4meeAH724@xxxxxxxxxxxxxxxx> wrote:
> 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/
>
>


--
regards
babelex


#OpenSource means free to innovate#



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise