User Tools

Site Tools


k7:k7.4:k7.4.9:k7.4.9.3:start

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 core of the new approach is first described as a class in which only the public variables required for the purpose of the application are declared with the required data type. In this approach, the class represents a record in a price list.
  • This class contains beside the (pure) data structure only one special method.
  • The method Object[]. sort (Mode As Integer) sorts objects, i. e. it calls the object-internal special method _compare (..) to compare the objects. Therefore, it is possible to sort an array whose elements are objects - which is true for derived arrays.
  • The advantage of the following approach is also that, in addition to specifying the sorting order, you can also specify the element in the object (static) according to which the object array is sorted step by step.

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:

  • The function properties of the _compare (DS As CDS) function must be defined according to the task.
  • An object of class type (–> CDS) is used as argument of the function.
  • Depending on the selector value, lines 10 to 17 of the select case control structure are sorted either by surname (1) or by age (2).
  • The result of the comparison must be returned as an integer in lines 12 (string comparison) and 14 (integer comparison):
+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.
  • If the _compare(..) method cannot be implemented for the objects, the objects are compared according to their addresses in memory. In this case, it makes sense to react with a corresponding error message (lines 15 and 16).

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 GridView is used to display the original data and sorted GridView data.
  • The GridToArray () function and the procedure ArrayToGrid (aArray As CDS[]) are used to transform the GridView data into an array - which can be sorted later - and from the original or sorted array into GridView data.
  • Line 89 calls the sorting of the derived array of type CDS[], which internally calls the special sorting method for object arrays in class CDS (line 6). The selector specifies the object variable to be used for sorting.

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.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.4/k7.4.9/k7.4.9.3/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools