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

Olivier Coquet ocoquet at 3d-phenomen.fr
Thu Jan 21 19:20:59 CET 2021


Here is the code I use all days(copy and paste directly from my one code):

Public Function ColSort(col As Collection[], ColKey As String, trie As 
Integer) As Collection[]
    Dim tmp_col As Collection[] = New Collection[col.count]
    Dim tmp_ar As String[] = New String[]

    For i As Integer = 0 To col.count - 1
       tmp_ar.Add(col[i][Colkey] & Chr(26) & i)
    Next

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

And it work !

And I don't understand why you are saying that collections in an array 
shouldn't be sorted, it doesn't make sense ?

in an array of collections, there is an index for each collection. If 
array is "ColArray"

We can retrieve the 5eme collection with: ColArray[5] <==5 is the index 
and all array have an index.

Friendly

Olivier

Le 21/01/2021 à 18:53, Jussi Lahtinen a écrit :
> I believe you have working code, but it definitely is NOT the one you 
> posted. It does not work.
> Moreover, collections shouldn't be sorted. They are not indexed.
>
> Jussi
>
>
> On Thu, Jan 21, 2021 at 9:39 AM Olivier Coquet <ocoquet at 3d-phenomen.fr 
> <mailto:ocoquet at 3d-phenomen.fr>> wrote:
>
>     Sorry my English is not very good,
>     In fact, this function does not sort the array but the collections it
>     contains.
>     In other words, if a collection array contains several
>     collections, the
>     collections are not necessarily in order.
>     For example:
>     let's imagine an array that contains three collections composed of
>     three
>     key/value pairs (name/surname/town).
>     This function will sort the collections in alphabetical order on
>     any key.
>     If you want to have the collections in the order of towns, just
>     specify
>     "town" in the function call, and the collections will be sorted on
>     the
>     key "town".
>     As far as I know, no Gambas function does that, and yet it is very
>     convenient.
>
>     Friendship
>     Olivier
>
>     Le 20/01/2021 à 19:57, Jussi Lahtinen a écrit :
>     > No, I meant this; col[Colkey]. The Colkey as string.
>     > And no, look more closely, what I said holds.
>     >
>     > Jussi
>
>     ----[ 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/20210121/2947b183/attachment.htm>


More information about the User mailing list