Sort an array of user-efined data type

csam63
csam63 used Ask the Experts™
on
I have an array of UDT with 4 elements:First Name,Last Name, MonthlySales, Commission. Have someone any ideea how can I sort descending by MontlySales and also to have for output the rest of the elements of the array,like FirstName...LastName...MontlySales
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can do the following

--- (writing in reply window... so check for errors)
Dim rsSortit As New ADODB.Recordset
Dim i As Integer
rsSortit.Fields.Append "FName", adVarChar
rsSortit.Fields.Append "LName", adVarChar
rsSortit.Fields.Append "Sale", adVariant
rsSortit.Fields.Append "Comm", adVariant
rsSortit.Open
For i = 0 To UBound(yourArray)
    rsSortit.AddNew
    rsSortit("FName") = yourArray(i).Name
    rsSortit("LName") = yourArray(i).LName
    rsSortit("Sale") = yourArray(i).Sale
    rsSortit("Comm") = yourArray(i).Comm
Next
rsSortit.Sort = "Sale desc"
'done
'----

This is still working even if you have HUGE amounts of data (100.000+) - If you do you would have to change i to long of course ;)

Author

Commented:
For rdrunner. This is not working for me. I'm storing the array in a sequential data file, because it doens't have to many records. Do you sugest me for this case?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Type UDT
    FirstName As String
    LastName As String
    MonthlySales As Double
    Commission As Double
End Type

Sub BubbleSort(UDTArray() As UDT)
    Dim lLB As Long
    Dim lUB As Long
    Dim udtTmp As UDT
    Dim I As Long
    Dim J As Long

    lLB = LBound(UDTArray)
    lUB = lubound(UDTArray)

    For I = lUB To lLB + 1 Step -1
        For J = lLB To I - 1
            If UDTArray(J).MonthlySales > UDTArray(J + 1).MonthlySales Then
                udtTmp = UDTArray(J + 1)
                UDTArray(J + 1) = UDTArray(J)
                UDTArray(J) = udtTmp
            End If
        Next
    Next
End Sub

Author

Commented:
For TigerZhao ...this is what I have now. The problem is when I want to print a report based on this array of UDT with FirstName...lastName...MontlySales, just the MontlySales is sorted corectly and the other variables not, they don't have the same subscript like MontlySales.Have any ideea why the output is not correct?
Function UDT(FirstName As String, LastName As String, MonthlySales As Double, Commision As Double) As UDT
    With UDT
        .FirstName = FirstName
        .LastName = LastName
        .MonthlySales = MonthlySales
        .Commission = Commision
    End With
End Function

Sub Main()
    Dim UDTArray(2) As UDT
    Dim I As Long
   
    UDTArray(0) = UDT("Michael", "Jackson", 30, 0.5)
    UDTArray(1) = UDT("Michael", "Jordan", 11, 0.7)
    UDTArray(2) = UDT("Bill", "Gates", 55, 0.1)
   
    BubbleSort UDTArray
   
    For I = 0 To 2
        With UDTArray(I)
            Debug.Print .FirstName, .LastName, .MonthlySales, .Commission
        End With
    Next
End Sub
== Immediate ==
Michael       Jordan         11            0.7
Michael       Jackson        30            0.5
Bill          Gates          55            0.1
====
is this you want?

Author

Commented:
I allready got it. Thanks anyway.

Commented:
Well i like ado recordsets for all my sorting needs. Can you tell me why it is not working ?

i usually dont have problems with them

Author

Commented:
I'm not saying that your code is not good. It could be great and I'm sure it is. Telling you the truth I'm a beginner in VB and I'm not at that level yet. Second, I have started to work in the same way like TigerZhao...I just omited somethig from my code and I couldn't figure out what. Anyway, I'm going to try your code because it looks more professional than mine. I'm sure that you think that I gave very easy to much points for this question....this is my first question on this site. Thanks a lot for your help

Commented:
There is also another advantage to using a recordset...

If you can assign it to most controls at runtime, and display all your data on a form with just one line of code.

Author

Commented:
It sounds more interesting that what I did. Maybe you can give more advices how to use it? I understood that on this site is chat rooms where users can meet and discuss. Do you know how can I login on this chat room? Thanks
Commented:
I dont know where there are some chatrooms here...

but try the following (fixed a bug in my code)

Put a datagrid (you find it under components) on your form - default name is datagrid1

Put a command button on your form
----
put this code in your command button:

Private Sub Command1_Click()
Dim rsSortit As New ADODB.Recordset
Dim i As Integer
rsSortit.Fields.Append "FName", adVariant
rsSortit.Fields.Append "LName", adVariant
rsSortit.Fields.Append "Sale", adVariant
rsSortit.Fields.Append "Comm", adVariant
rsSortit.Open
For i = 0 To UBound(yourArray)
   rsSortit.AddNew
   rsSortit("FName") = yourArray(i).Name
   rsSortit("LName") = yourArray(i).LName
   rsSortit("Sale") = yourArray(i).Sale
   rsSortit("Comm") = yourArray(i).Comm
Next
rsSortit.Sort = "Sale desc"
'done
set datagrid1.datasource = rssortit
end sub

Author

Commented:
thanks...I'll try it later and I'll let you know how it's working

Author

Commented:
Now I got an other error "User-defined type not defined" and it's refering to the ADODB.Recordset

Commented:
you need a refference to MS ado data objects

Author

Commented:
rdrunner...it works. thanks a lot!

Commented:
Your welcome...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial