Visual Basic 6 Problem

Miscellaneous Forums/General Discussion/Visual Basic 6 Problem

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Ah - now I see: you want to sort your contacts array by whatever field the user selects via the combo? Hmmm. First problem is that your sort code is totally wrong. There are several methods you could use, but the following should do what you want:
Option Explicit

Type Contact
  first_name As String
  last_name As String
  text As String
  sort_key As String
End Type

Dim contact_arr(20) As Contact

Private Sub Main()
  With contact_arr(1)
    .text = "text 1"
    .first_name = "First Name 1"
    .last_name = "Last Name 1"
  End With
  Call SortContacts(contact_arr())
End Sub

Private Sub SortContacts(ArrayList() As Contact)
  Dim SortType As Long
  Dim k As Long
  
  SortType = Form1.Combo1.ListIndex
  For k = 0 To UBound(ArrayList)
    Select Case SortType
    Case 0 ' whatever your 1st dropdown entry is: eg. first_name
      ArrayList(k).sort_key = ArrayList(k).first_name
    Case 1 ' whatever your 2nd dropdown entry is: eg. last_name
      ArrayList(k).sort_key = ArrayList(k).last_name
    ' add other Case statements for each field - ordered to match the combo dropdown list
    End Select
  Next k
  Call QuickSortContacts(contact_arr(), 0, UBound(contact_arr))
End Sub

Private Sub QuickSortContacts(ArrayList() As Contact, ALbound As Long, AUbound As Long)
  Dim ArrayMid As Contact
  Dim ArrayBuffer As Contact
  Dim CurLow As Long
  Dim CurHigh As Long
  Dim CurMidpoint As Long
  
  If AUbound > ALbound Then
    CurLow = ALbound
    CurHigh = AUbound
    CurMidpoint = (ALbound + AUbound) \ 2
    ArrayMid = ArrayList(CurMidpoint)
    Do While (CurLow <= CurHigh)
      Do While ArrayList(CurLow).sort_key < ArrayMid.sort_key
        CurLow = CurLow + 1
        If CurLow = AUbound Then
          Exit Do
        End If
      Loop
      Do While ArrayMid.sort_key < ArrayList(CurHigh).sort_key
        CurHigh = CurHigh - 1
        If CurHigh = ALbound Then
          Exit Do
        End If
      Loop
      If (CurLow <= CurHigh) Then
        ArrayBuffer = ArrayList(CurLow)
        ArrayList(CurLow) = ArrayList(CurHigh)
        ArrayList(CurHigh) = ArrayBuffer
        CurLow = CurLow + 1
        CurHigh = CurHigh - 1
      End If
    Loop
    If ALbound < CurHigh Then
      Call QuickSortContacts(ArrayList(), ALbound, CurHigh)
    End If
    If CurLow < AUbound Then
      Call QuickSortContacts(ArrayList(), CurLow, AUbound)
    End If
  End If
End Sub

As you can see I have added field 'sort_key' to your Contact type, and populate this using your ComboBox's ListIndex value via subroutine SortContacts (obviously you will need to change 'Form1.Combo1.ListIndex' accordingly!!). I then call QuickSortContacts which sorts the array based on whatever value is stored in field 'sort_key'.

Let me know if you need any more help.