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

Olivier Coquet ocoquet at 3d-phenomen.fr
Wed Jan 20 08:20:54 CET 2021


Hi Jussi,

This make sense :)

look more, CInt(Split(tmp_ar, Chr(26), "", True)[1]) is an integer not a 
string, and try the function, you'll see than it do the job.

Friendly,

Olivier

Le 19/01/2021 à 22:34, Jussi Lahtinen a écrit :
> 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 
> <mailto: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
>     <http://gambaswiki.org/wiki/doc/netiquette> ]----
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210120/e29adcdc/attachment-0001.htm>


More information about the User mailing list