[Gambas-user] Sorting 2D arrays

John Rose john.aaron.rose at mailbox.org
Wed Aug 26 08:32:32 CEST 2020


All the discussions on this topic have gone way over my knowledge & 
understanding, particularly the discussions on Class. I was wrong about 
Gianluigi's solution (on 24/08/2020, 08:43 digest for 
DinamixMatrixExample project from the farm) not working. Apologies, 
Gianluigi, for doubting you. Once I understood it, I adapted it slightly 
for the key on my 2D array and it worked. I doubt that anyone is 
interested now, but here is my adapted code for the Global variable 
aEpisodeData (defined as Private aEpisodeData As New String[][]):

Procedure SortEpisodeData()' Sort by Series Name & Episode Name i.e. 
column 0
   Dim e, i, n As Integer
   Dim aSort, aOriginal As New String[]
   ' Fills the array of the key to sort
   For e = 0 To aEpisodeData.Max
     aOriginal.Push(aEpisodeData[e][0])
   Next
   ' Copy the data in the sorting array, then sort
   aSort = aOriginal.Copy()
   aSort.Sort()
   ' Find with "Find" the values to be sorted and' with
   ' "swap" swaps reversing them
   For e = 0 To aSort.Max
     n = aOriginal.Find(aSort[e], 0, e)
     For i = 0 To aEpisodeData[e].Max
       Swap aEpisodeData[e][i], aEpisodeData[n][i]
     Next
     ' Clean and fill the array with current data
     aOriginal.Clear()
     For i = 0 To aEpisodeData.Max
       aOriginal.Push(aEpisodeData[i][0])
     Next
   Next
End ' SortEpisodeData

where aEpisodeData has previously been populated by:
     ' All the other variables below are strings previously given values
     ' Column 0 of aEpisodeData is the key of a row
     'Columns 1-7 are the data for each row
     Inc iRows  ' Number of rows in aEpisodeData defined as Integer
     aEpisodeData.Resize(iRows)
     aEpisodeData[iRows - 1] = New String[8]
     aEpisodeData[iRows - 1][0] = sSN30 & "|" & sEN30  ' SN30 & sEN30 
are the first 30 characters of sSeriesName & sEpisodeName respectively 
padded out with spaces
     aEpisodeData[iRows - 1][1] = sSeriesName
     aEpisodeData[iRows - 1][2] = sSeriesDescription
     aEpisodeData[iRows - 1][3] = sEpisodeName
     aEpisodeData[iRows - 1][4] = sEpisodeNumber
     aEpisodeData[iRows - 1][5] = sEpisodeDescription
     aEpisodeData[iRows - 1][6] = sEpisodeDatePublished
     aEpisodeData[iRows - 1][7] = sEpisodeIdentifier






More information about the User mailing list