Link to home
Start Free TrialLog in
Avatar of Kev
KevFlag for Australia

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kev

ASKER

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.
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
Avatar of Kev

ASKER

Hey ~IM

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

Budorat
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