User Tools

Site Tools


k7:k7.4:k7.4.9:k7.4.9.1:start

7.4.9.1 Sorting one-dimensional arrays

7.4.9.1.1 Example 1 - Sorting a one-dimensional native array

Sorting the elements of one-dimensional native arrays works with the sort(…)method without any problems - as the following example shows:

Public Sub btnNativesArraySortieren_Click()
  Dim iCount, k As Integer
  Dim a1DArray As New String[]
  Dim aNames As String[] = ["Eagle", "Bear", "Badger", "Fox", "Tit", "Eagle Owl", "Moose"]

  iCount = 5
  a1DArray.Resize(iCount)

  Randomize

  For k = 0 To a1DArray.Bounds[0] - 1
    a1DArray[k] = aNames[Int(Rnd(0, aNames.Count))]
  Next ' k

  ShowElements(a1DArray)
    a1DArray.Sort(gb.Ascent) ' Sorting standard a..z
  ShowElements(a1DArray)
    a1DArray.Reverse()
  ShowElements(a1DArray)

End ' btnNativesArraySortieren_Click()

Public Sub ShowElements(aArray As String[])
  Dim sElement As String

  For Each sElement In aArray
    Print sElement,
  Next ' sElement
  Print

End ' ShowElements(aArray As String[])

Displays the elements of the a2DArray array in the console of the Gambas IDE:

Bear    Eagle   Owl     Tit     Fox     → Original
Eagle   Bear    Fox     Tit     Owl     → Sorted in ascending order
Owl     Tit     Fox     Bear    Eagle   → Sorted elements inverted sorted sorted

Hint:

The rearranging of elements in an array with the method Array.Reverse() can be seen as a special case of sorting an array. All elements from[E0 to Ek] in the array are rearranged to[Ek to E0].

7.4.9.1.2 Example 2 - Sorting a one-dimensional derived array

The names of all control elements of the' Label' type are to be explored and sorted on the form. The following idea for implementation sounds plausible:

  • First, all control elements of type Label are captured and the labels found are stored as an object in a derived array of aLabels of type Label[].
  • Then the array aLabels (ascending) is sorted.
  • Then the label names of the sorted labels are read and displayed in the array aLabels.

The following source code is used:

Public Sub btnSortLabel1_Click()
  Dim k As Integer
  Dim lLabel As Label
  Dim aLabels As New Label[]
  Dim hControl As Control
 
  For Each hControl In Me.Children
    If Object.Type(hControl) = "Label" Then
       Inc k
       aLabels.Resize(k)
       aLabels[k - 1] = hControl
    Endif
  Next ' hControl
 
  For Each lLabel In aLabels ' For Control
    Print lLabel.Name
  Next
  aLabels.Sort(gb.Ascent)
  For Each lLabel In aLabels
    Print lLabel.Name
  Next
 
End ' btnSortLabel1_Click()

For control purposes, the names of the labels are first output from the original, unsorted array. This result was shown in the console of the IDE:

lblMultiArray
lblClassArray
lblLabels
lblDemonstration

The order of the elements in the aLabels array corresponds to the order of the individual labels in the hierarchy of visible controls in the Gambas IDE. You can't be satisfied with the output of the names of the labels from the sorted array aLabels:

lblClassArray
lblLabels
lblMultiArray
lblDemonstration

Sorted looks different! The reason for this is that the elements in an array of the type Label[] - as a derived array - are compared by their addresses in memory, since the class Label does not implement a _compare () method.

The original idea is modified for this reason:

  • First, all controls of type Label are captured and from all labels found, the label names are immediately stored in an array of aLabelNames of type String[].
  • Then the native array aLabelNames (ascending) is sorted.
  • Finally, the label names are read and displayed from the sorted array aLabelNames.

The modified source code can be read here:

Public Sub btnSortLabel2_Click()
  Dim k As Integer
  Dim sName As String
  Dim aLabelNames As New String[] ' Native Array
  Dim hControl As Control
 
  For Each hControl In Me.Children
    If Object.Type(hControl) = "Label" Then
       aLabelNames.Add(hControl.Name)
    Endif
  Next ' hControl
  For Each sName In aLabelNames ' For Control
    Print sName
  Next
  Print
  aLabelNames.Sort(gb.Ascent)
  For Each sName In aLabelNames
    Print sName
  Next
End ' btnSortLabel2_Click()

The console now displays the correct list of ascending sorted label names:

lblClassArray
lblDemonstration
lblLabels
lblMultiArray
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.1/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools