Collection asSortedCollection

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Collection asSortedCollection

Ankh'nAton
Hi!

I needed sorting and found

   Array sort: aSortBlock

So I added asSortedCollection to the Collection class like...

  asSortedCollection
        ^self sort: [ :a :b | a asString < b asString ].

It does a string compare on the collections contents and it works. Any
complains about this aproach?

Cheers...
Reply | Threaded
Open this post in threaded view
|

Re: Collection asSortedCollection

Nicolas Petton
#sort: does it in place. I would use #sorted: (which does a copy) if you
want a behavior similar to #asSortedCollection:

I would convert the collection to an Array first, since only Array has
the sorting methods. also, why do you use #asString for the comparison?

Collection >> asSortedCollection
        ^self class new
                addAll: self asArray sorted;
                yourself

Collection >> asSortedCollection: aBlock
        ^self class new
                addAll: (self asArray sorted: aBlock);
                yourself


_but_ the selector #asSortedCollection would mean that the answered
object is an instance of SortedCollection, which is not present in Amber.

Cheers,
Nico

On 24/02/12 16:37, Tom wrote:

> Hi!
>
> I needed sorting and found
>
>     Array sort: aSortBlock
>
> So I added asSortedCollection to the Collection class like...
>
>    asSortedCollection
> ^self sort: [ :a :b | a asString<  b asString ].
>
> It does a string compare on the collections contents and it works. Any
> complains about this aproach?
>
> Cheers...


--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Collection asSortedCollection

Ankh'nAton
Thank You Nicolas!

I knew I made a very quick choice. That's why I asked here. Not
getting an instance of a true SortedCollection class is ok. I always
ever only used #asSortedCollection to get a sorted representation of
an unsorted Collection and that's what it does now.

Thanks again...


On 24 Feb., 17:07, Nicolas Petton <[hidden email]> wrote:

> #sort: does it in place. I would use #sorted: (which does a copy) if you
> want a behavior similar to #asSortedCollection:
>
> I would convert the collection to an Array first, since only Array has
> the sorting methods. also, why do you use #asString for the comparison?
>
> Collection >> asSortedCollection
>         ^self class new
>                 addAll: self asArray sorted;
>                 yourself
>
> Collection >> asSortedCollection: aBlock
>         ^self class new
>                 addAll: (self asArray sorted: aBlock);
>                 yourself
>
> _but_ the selector #asSortedCollection would mean that the answered
> object is an instance of SortedCollection, which is not present in Amber.
>
> Cheers,
> Nico
>
> On 24/02/12 16:37, Tom wrote:
>
> > Hi!
>
> > I needed sorting and found
>
> >     Array sort: aSortBlock
>
> > So I added asSortedCollection to the Collection class like...
>
> >    asSortedCollection
> >    ^self sort: [ :a :b | a asString<  b asString ].
>
> > It does a string compare on the collections contents and it works. Any
> > complains about this aproach?
>
> > Cheers...
>
> --
> Nicolas Pettonhttp://nicolas-petton.fr