Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Defined TYPE sorting?

Status
Not open for further replies.

mpdillon

Programmer
Jan 27, 2004
18
0
0
US
Hi,
Is there a simple way to sort the following User Defined Type on OrdNo?

Public Type Orders
OrdNo as String
InvNo as string
End Type

Sub Main
dim Order() as Orders

ReDim Order(3)

Order(0).OrdNo = "3"
Order(0).InvNo = "556"
Order(1).OrdNo = "1"
Order(1).InvNo = "333"
Order(2).OrdNo = "4"
Order(2).InvNo = "251"
Order(3).OrdNo = "0"
Order(3).InvNo = "952"

'How to Sort by Order().OrdNo???

Previously and without using a User Defined Type, I would have assinged the OrdNo and InvNo to the same element in an array and then sorted an array.
strArray(0) = Left("3" & space(20), 20) & left("556" & space(20), 20)
strArray(1) = ...
strArray(2) = ...
strArray(3) = ...
'
SortArray strArray()

Any ideas would be appreciated.
thanks,
pat
 
How about something like this (simple bubble sort)

Code:
Dim tindex As Integer
Dim isSorted As Boolean
Dim tempvalue As String

Do
  isSorted - True
  For tindex = LBound(arrName) + 1 To UBound(arrName)
    If arrName(tindex).FieldName < arrName(tindex - 1).FieldName Then
      tempvalue = arrName(tindex).FieldName
      arrName(tindex).FieldName = arrName(tindex - 1).FieldName
      arrName(tindex - 1).FieldName = tempvalue
      isSorted = False
    End If
  Next
Loop While isSorted = False

Lee
 
<[tt]isSorted - True[/tt]
Has a typo. It should be
[tt]isSorted = True[/tt]

Of course, you could also use ADO Recordsets for this, and just set the Sort property.

Bob
 
Yup, you caught my typo. I also had another error in the data type that was swapped:

Code:
If arrName(tindex).FieldName < arrName(tindex - 1).FieldName Then
  tempvalue = arrName(tindex).FieldName
  arrName(tindex).FieldName = arrName(tindex - 1).FieldName
  arrName(tindex - 1).FieldName = tempvalue
  isSorted = False
End If

should have been:

Code:
Dim tempvalue As Orders

If arrName(tindex).FieldName < arrName(tindex - 1).FieldName Then
  tempvalue = arrName(tindex)
  arrName(tindex) = arrName(tindex - 1)
  arrName(tindex - 1) = tempvalue
  isSorted = False
End If

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top