7.4.9.3 Sorting derived arrays

In order to correctly implement the idea of sorting data in a GridView, as in the example in chapter 7.4.9.2, it is obviously necessary to have a different way of mapping the data of the GridView to an array that can be sorted correctly.

Approach:

The complete source texts for the class CDS (class DataSet) and for the main program are specified here and commented on briefly:

Class CDS.class

[1] ' Gambas class file
[2]
[3] Public Surname As String
[4] Public Age As Integer
[5]
[6] Public Function _compare(DS As CDS) As Integer
[7]   Dim iSelector As Integer
[8]
[9]   iSelector = 1
[10]   Select Case iSelector
[11]   Case 1
[12]     Return Comp(Surname, DS.Surname)
[13]   Case 2
[14]     Return Sgn(Age - DS.Age)
[15]   Default
[16]       Error.Raise(("No suitable sorting method found!"))
[17]   End Select
[18]
[19] End

Comments:

+1 , if the object is larger than the argument passed,
0  , if the objects are equal,
-1 , if the object is smaller than the argument passed.

Source code for FMain. class

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

Comments:

The result is impressive:

B1 B2
Figure 7.4.9.3.1: Correct sorting of GridView data

Chapter –> '7.4.9.4 Project for sorting arrays' introduces you to a project in which the element dynamic can be defined, according to which the object array is sorted.