7.4.9.2 Sorting a multidimensional native array

The Array-Sort () method can also be applied to multidimensional arrays - but the results are not as you might expect!

In a GridView, a name (1st field) and age are entered in the individual lines. Then all rows in the GridView should be sorted by name. Good to know that a GridView has the property GridView. Sorted. However, the GridView documentation states that you have to develop the necessary sorting algorithms yourself. It germinates the idea to store all fields of the GridView in a string array, to sort the array and to read the sorted array back into the GridView. Thought - done. For example, in the elements of a static, two-dimensional string array of type String[5,2], these GridView data are entered as a character string, since all fields in a GridView are of data type' String':

[ 0 | 0 ] = Fox
[ 1 | 0 ] = Badger
[ 2 | 0 ] = Bear
[ 3 | 0 ] = Owl
[ 4 | 0 ] = Eagle
[ 0 | 1 ] = 16
[ 1 | 1 ] = 15
[ 2 | 1 ] = 16
[ 3 | 1 ] = 18
[ 4 | 1 ] = 15

The formatted representation of the elements of the array in a GridView (? procedure ArrayToGrid (..)) then looks like this:

B1
Figure 7.4.9.2.1: ArrayToGridView

The source text is completely specified:

[1] Private $2DGridArray As String[]
[2]
[3] Public Sub Form_Open()
[4]   FMain.Center
[5]   FMain.Resizable = False
[6]   SetGridProperty()
[7]   FillGridView()
[8]   $2DGridArray = GridToArray()
[9] End ' Form_Open()
[10]
[11] Private Sub SetGridProperty()
[12]   GridView1.Header = GridView1.Both ' Header line is displayed
[13]   GridView1.Mode = Select.Single ' The selected line is highlighted in colour
[14]   GridView1.Columns.Count = 2 ' Number of lines
[15]   GridView1.Sorted = True
[16]   GridView1.Columns[0].Width = 140
[17]   GridView1.Columns[0].Alignment = Align.Center
[18]   GridView1.Columns[0].Title = "Surename"
[19]   GridView1.Columns[1].Width = 60
[20]   GridView1.Columns[1].Alignment = Align.Center
[21]   GridView1.Columns[1].Title = "Age"
[22] End ' SetGridProperty()
[23]
[24] Public Sub FillGridView()
[25]   Dim iCount As Integer
[26]   Dim aNames As String[] = ["Eagle", "Bear", "Badger", "Fox", "Tit", "Owl", "Moose"] '
[27]
[28]   Randomize
[29]   For iCount = 0 To 4 ' Data sets with random data
[30]     Inc GridView1.Rows.Count ' Display of generated data records (lines in grid view)
[31]     GridView1.MoveTo(GridView1.Rows.Count - 1, 0) ' Optional
[32]     GridView1[iCount, 0].Text = aNames[Int(Rnd(0, aNames.Count))]
[33]     GridView1[iCount, 1].Text = Str(Int(Rnd(15, 20))) ' 15..19
[34]   Next ' iCount'
[35]
[36] End ' FillGridView()
[37]
[38] Public Function GridToArray() As String[]
[39]   Dim i, j As Integer
[40]   Dim a2DGridArray As New String[5, 2] ' 5 lines, 2 columns
[41]
[42]   For i = 0 To GridView1.Rows.Max
[43]     For j = 0 To GridView1.Columns.Max
[44]       a2DGridArray[i, j] = GridView1[i, j].Text
[45]       ' Print i, j, a2DGridArray[i, j]
[46]     Next ' j
[47]   Next ' i
[48]
[49]   Return a2DGridArray
[50]
[51] End ' Function GridToArray()
[52]
[53] Public Sub ArrayToGrid(aArray As String[])
[54]   Dim i, j As Integer
[55]
[56]   GridView1.Clear
[57]   GridView1.Rows.Count = aArray.Bounds[0]
[58]   For i = 0 To aArray.Bounds[0] - 1
[59]     For j = 0 To aArray.Bounds[1] - 1
[60]          GridView1[i, j].Text = aArray[i, j]
[61]     Next ' j
[62]   Next ' i
[63]
[64] End ' ArrayToGrid(..)
[65]
[66] Public Sub GridViewShow(Optional bSorted As Boolean)
[67]   Dim aGArray As New String[]
[68]
[69]   aGArray = $2DGridArray.Copy()
[70]   If bSorted = True Then
[71]      aGArray.Sort
[72]      ArrayToGrid(aGArray)
[73]   Else
[74]     ArrayToGrid($2DGridArray)
[75]   Endif
[76]
[77] End ' GridViewShow(..)
[78]
[79] Public Sub btnGridViewShow_Click()
[80]   GridViewShow()
[81] End ' btnGridViewShow_Click()
[82]
[83] Public Sub btnGridViewShowS_Click()
[84]   GridViewShow(True)
[85] End ' btnGridViewShowS_Click()

Comments:

The result is sobering - as the display of the elements in the GridView and IDE console shows - although the elements of the two-dimensional array are correctly sorted:

B2
Figure 7.4.9.2.2: Display of elements of the sorted array in the GridView

[ 0 | 0 ] = 15
[ 1 | 0 ] = 15
[ 2 | 0 ] = 16
[ 3 | 0 ] = 16
[ 4 | 0 ] = 18
[ 0 | 1 ] = Eagle
[ 1 | 1 ] = Bear
[ 2 | 1 ] = Fox
[ 3 | 1 ] = Badger
[ 4 | 1 ] = Owl

An acceptable solution is presented in chapter 7.4.9.3 Sorting derived arrays.