[Gambas-user] Sorting 2D arrays

Charlie Ogier charlie at cogier.com
Mon Aug 24 17:59:20 CEST 2020


Hi John,

My solution is probably not as 'pure' as Gianluigi's but there is a lot 
less code. My solution to this is to create 1 array from all the other 
arrays.
e.g. aAllData [] =
"aSeriesName0, aEpisodeName0, aEpisodeNumber0, ..."
"aSeriesName1, aEpisodeName1, aEpisodeNumber1, ..."
etc....

To sort the array I add another copy of the array to sort at the 
beginning of this array e.g.
" aEpisodeNumber0, aSeriesName0, aEpisodeName0, aEpisodeNumber0, ..."
" aEpisodeNumber1, aSeriesName1, aEpisodeName1, aEpisodeNumber1, ..."
etc....

I then sort the array and remove the 'extra' array.
Now you can just split the array to display or manipulate it.

You can add to this array by adding all the components separated by a 
comma (or another separator).

Have a look at the attached program.

I hope it helps.

Charlie

On 24/08/2020 08:02, John Rose wrote:
> I have 7 string arrays defined as follows:
> Private aSeriesName As String[]
> Private aEpisodeName As String[]
> Private aEpisodeNumber As String[]
> Private aEpisodeDescription As String[]
> Private aEpisodeDatePublished As String[]
> Private aEpisodeIdentifier As String[]
> Private aSeriesDescription As String[]
>
> Each array is populated in a loop from a similarly named string 
> variable e.g. aSeriesName.Add(sSeriesName).
>
> The ith element of each array are correspondent to each other (i.e. 
> like a Relational Database row).
>
> You could visualise the above (thus replacing the above arrays) as 
> equivalent to a 2D array aData defined as 'Private[7, 1000] - I used 
> 1,000 as I couldn't work out how to have a dynamic 2D array where one 
> 'index' is fixed: can that be done?
>
> Next problem with aData is there seems to be no Add method for a 2D 
> array. Any ideas? Only idea that I've had is to specifically set an 
> element of Data using a counter which is incremented by 1 for each 
> element's value setting: something like aData[0, iCounter]=sSeriesName
>
> I would like to sort aData using 2 of the 'fields' (SeriesName being 
> major & EpisodeName being minor). I've noticed that there is a Sort 
> method for a 1D array but not for a 2D array. Any ideas?
>
> PS apologies if my explanation is not clear.
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ArraySort-0.0.1.tar.gz
Type: application/gzip
Size: 13113 bytes
Desc: not available
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20200824/88808c8b/attachment-0001.gz>


More information about the User mailing list