[Gambas-user] A piece of code to sort an array of collections

Jussi Lahtinen jussi.lahtinen at gmail.com
Tue Jan 19 22:34:14 CET 2021


I don't understand what you mean, so I looked at the code...
col[] should take integer, not string, as it is an array of collections.
Also this doesn't make sense at all (unnecessary loop or missing index &
add):

For i = 0 To tmp_ar.count - 1
tmp_col = col[CInt(Split(tmp_ar, Chr(26), "", True)[1])]
Next


Jussi

On Tue, Jan 19, 2021 at 7:20 PM Olivier Coquet <ocoquet at 3d-phenomen.fr>
wrote:

> A little piece of code for anyone wondering how to sort a collection table
> on one of its keys.
> example:
>
> dim macollection as collection[] = new collection[]
>
> macollection.add([
> "clef1": "nimporte quoi",
> "clef5": "nimporte quoi aussi"
> ])
>
> macollection.add([
> "clef1": "1er test",
> "clef5": "zzzzz"
> ])
>
> This array of collections has two elements (collections) each composed of
> two keys (key1 and key5)
> If they are listed, they will be in the order above.
> Let's use this code:
>
>
> Public Function ColSort(col As Collection[], ColKey As String, trie As
> Integer) As Collection[] 'Trie d'un tableau de collection sur la clef
> demandée
> Dim tmp_col As Collection[] = New Collection[col.count] 'un tableau de
> collection temporaire pour les manips
> Dim tmp_ar As String[] = New String[] 'un tableau de chaînes pour les
> manips
>
> For i As Integer = 0 To col.count - 1 'on parcoure les collections et on
> récupère
> tmp_ar.Add(col
>
>
>
>
>
>
>
> *[Colkey] & Chr(26) & i) 'La valeur de clef et le numéro d'ordre original
> Next tmp_ar.Sort(trie) 'on trie le tableau obtenu For i = 0 To tmp_ar.count
> - 1 'on parcour le tableau trié tmp_col = col[CInt(Split(tmp_ar, Chr(26),
> "", True)[1])] 'on ré-attribue chaque collection dans le bon ordre Next
> Return tmp_col 'on retourne le tableau de collection trié End*
>
> *We call the function like this:*
>
>
> *macollection=ColSort(macollection, "clef1", gb.Ascent)*
>
>
>
>
>
>
> *And we get an array of collections sorted on the key "clef1", in
> ascending order. Note that the function will work for any key provided, as
> long as it exists in the collections. Magic! Friendship Olivier *
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210119/e37228c1/attachment-0001.htm>


More information about the User mailing list