groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinko Srko─Ź <dinko.sr...@gmail.com>
Subject Re: [groovy-user] Remove duplications from list of lists
Date Tue, 21 Apr 2015 09:48:07 GMT
This should work:

  data.unique { a, b -> a[0] <=> b[0] ?: a[3] <=> b[3] }

The "spaceship" operator actually desugars to `compareTo` - that's why
you couldn't use it on Lists.

Also, be careful when using `unique(Closure)`, as this operation
mutates the original list. To get the new list with unique elements
you could use `unique(boolean, Closure)` with boolean parameter set to
`false`, or newly added `toUnique(Closure)`.

Finally, note that I have added to CC the new mailing list, this one
is soon to be switched off. On how to subscribe see here:
http://groovy-lang.org/mailing-lists.html

Cheers,
Dinko

On 21 April 2015 at 11:07, jreyeshdez <julianreyeshdez@gmail.com> wrote:
>
> Hello,
>
> I was wondering how I can achieve the following result using the method
> unique()
>
> I have the following list:
>
> [
> [name1,1,1,1800],[name2,1,1,800],[name2,1,1,1800],[name1,1,1,800],[name1,1,1,1800]
> ]
>
> And I would like to get the following:
>
> [ [name1,1,1,1800],[name2,1,1,800],[name2,1,1,1800],[name1,1,1,800] ]
>
> I tried to use:
>
> data.unique { a, b -> [a[0],a[3]] <=> [b[0],b[3]] }
>
> But it does not work..
>
>
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Remove-duplications-from-list-of-lists-tp5723394.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

Mime
View raw message