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

Jussi Lahtinen jussi.lahtinen at gmail.com
Thu Jan 21 19:33:40 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.

OK, this looks better. It is not the same code you posted earlier (if you
don't believe me, look yourself).
Collections are not guaranteed to keep order or so Benoit has said. I don't
know in what situation the order can change.
Maybe I have misunderstood that.
Collections have key, not index. The key can be a number, but it is still

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210121/2cbc41b5/attachment.htm>

More information about the User mailing list