[Gambas-user] Accessing the nth item in a collection

Tobias Boege taboege at ...626...
Wed Jun 4 13:59:24 CEST 2014


On Wed, 04 Jun 2014, Tobias Boege wrote:
> On Wed, 04 Jun 2014, Jussi Lahtinen wrote:
> > If I remember correctly Benoit warned that collections doesn't necessarily
> > keep the items in order. But I don't know what would make it to lose the
> > order. Maybe he will enlighten the issue.
> > Anyway I suggest you use array of objects, which you declare extra property
> > "Key".
> > 
> > On Wed, Jun 4, 2014 at 3:22 AM, B Bruen <bbruen at ...2308...> wrote:
> > 
> > > Occasionally, and unfortunately for me lately quite frequently, I have
> > > found a need to access the first or last item in (or perhaps better "the
> > > first or last item added to")  a collection regardless of key values (and
> > > by extension I could envisage a need to access the nth item in a collection
> > > regardless of the key value). There does not seem to be an elegant solution
> > > to this - or maybe just that I am incapable of developing an elegant
> > > solution to this.
> > >

Oh, I just saw the "added to" bit. That's not possible with AvlTree and also
won't be with Collection.

Then I suppose that you create a custom Collection class and make it record
the first and last items added. For the general case of the n-th added item,
you really need to sort your keys by the point in time they're inserted and
that's plainly an array using the Add() method.

Generally, if you want to have the same data *sorted* in different ways (and
you want "sorted" because otherwise you would need to loop through the data
which you don't want), it sounds plausible that you need the data twice, but
in diffent containers and sorted differently.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list