[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Sort One Dimensional Array of UDT prior to writing to file

I have an array that I need to sort. The array is one-dimensional, however, it has UDT elements. Code as follows:

I need to sort the array on opLastName prior to writing to a text file.

Public Type SalaryData
   opLastName As String
   opFirstName As String
   opHrsWorked As Double
   opNormalPay As Currency
   opOvertimePay As Currency
   opBonus As Currency
   opGrossPay As Currency
   opTaxation As Currency
   opNetPay As Currency
   opDateCreated As Date
End Type

Dim SalaryOutput() As SalaryData

'Write salary output data to array SalaryOutput
Private Sub MonthlySalaryReport()
   ReDim Preserve SalaryOutput(Index)
   With SalaryOutput(Index)
      .opLastName = txtSName.Text
      .opFirstName = txtFName.Text
      .opHrsWorked = txtHoursWorked.Text
      .opNormalPay = txtNormalPay.Text
      .opOvertimePay = txtOvertimePay.Text
      .opBonus = txtBonus.Text
      .opGrossPay = txtGrossPay.Text
      .opTaxation = txtTaxation.Text
      .opNetPay = txtNetPay.Text
      .opDateCreated = txtDateCreated.Text
   End With
   Index = Index + 1
End Sub

Kind thanks in advance

Budorat
0
Kev
Asked:
Kev
  • 4
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is how to bubble sort them:

Option Explicit

Private Type SalaryData
   opLastName As String
   opFirstName As String
   opHrsWorked As Double
   opNormalPay As Currency
   opOvertimePay As Currency
   opBonus As Currency
   opGrossPay As Currency
   opTaxation As Currency
   opNetPay As Currency
   opDateCreated As Date
End Type

Private SalaryOutput() As SalaryData

Private Sub Form_Load()
    ReDim SalaryOutput(4)
    SalaryOutput(0).opLastName = "Smith"
    SalaryOutput(0).opNormalPay = 0
    SalaryOutput(1).opLastName = "Jones"
    SalaryOutput(0).opNormalPay = 1
    SalaryOutput(2).opLastName = "Alda"
    SalaryOutput(0).opNormalPay = 2
    SalaryOutput(3).opLastName = "Tom"
    SalaryOutput(0).opNormalPay = 3
    SalaryOutput(4).opLastName = "Crum"
    SalaryOutput(0).opNormalPay = 4
End Sub

Private Sub Command1_Click()
    Debug.Print "Before Sort:"
    outputSalaryData
   
    bubbleSort
   
    Debug.Print "After Sort:"
    outputSalaryData
End Sub

Private Sub outputSalaryData()
    Dim i As Integer
    For i = LBound(SalaryOutput) To UBound(SalaryOutput)
        Debug.Print i & ": " & SalaryOutput(i).opLastName & ", " & SalaryOutput(i).opNormalPay
    Next i
End Sub

Private Sub bubbleSort()
    Dim i As Integer
    Dim j As Integer
    Dim temp As SalaryData

    For i = (UBound(SalaryOutput) - 1) To 0 Step -1
        For j = 0 To i
            If SalaryOutput(j).opLastName > SalaryOutput(j + 1).opLastName Then
                ' swap values
                temp = SalaryOutput(j)
                SalaryOutput(j) = SalaryOutput(j + 1)
                SalaryOutput(j + 1) = temp
            End If
        Next j
    Next i
End Sub
0
 
KevAuthor Commented:
Hey Idle Mind,

thanks for the help, I have used your code to fit into my program succesfully. There is one note however, if you have

SalaryOutput(3).opLastName = "de Vos"

it does not recognise that there is a lower case "d". How would i get around this? Would I have to change the string to uppercase prior to testing????

I guess I would have to have something like

format$(SalaryOutput(j).opLastName,"UCASE")

would this work, (i am trying it now) or is there a better option?

Thanks again.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could convert both last names to uppercase in the bubble sort compare statement like this:

    If UCase(SalaryOutput(j).opLastName) > UCase(SalaryOutput(j + 1).opLastName) Then

~IM
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KevAuthor Commented:
Hey ~IM

Thanks heaps for the help, i was almost there....... your solution is perfect.

Budorat
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No problem.

Bear in mind that using a bubble sort is s...l...o...w for large data sets.  I wouldn't recommend using it for anything above around 1000 elements (though it will still work).

For larger sets that are less than 10,000 elements, you can switch to quicksort.

For very large data sets, I recommend you use a database system.

Of course, these numbers are really dependent upon how fast the computer is and how much memory there is.  You be the judge as to what is right for your application.

~IM
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now