• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Sorting Arrays of User Defined Types

AV114

Member
I have an array of user-defined types. The type has three elements and I need to sort only one (value one)

Can anyone help on this

Private Type Demand
State As String
Pack As String
Code As String
Value As Long
End Type
 
You could use a simple bubble sort like this:

Code:
Option Explicit

Private Type Demand
    State As String
    Pack As String
    Code As String
    Value As Long
End Type

Sub sortThem()
    Dim a() As Demand
    ReDim a(1 To 3)
    With a(1)
        .Value = 4
        .Code = "Item1"
    End With
    With a(2)
        .Value = 0
        .Code = "Item2"
    End With
    With a(3)
        .Value = 1
        .Code = "Item3"
    End With
    MsgBox a(1).Code & ", " & a(2).Code & ", " & a(3).Code
    a = BubbleSortDemands(a)
    MsgBox a(1).Code & ", " & a(2).Code & ", " & a(3).Code
End Sub
Public Function BubbleSortDemands(data() As Demand) As Demand()

   Dim a As Long
   Dim e As Integer, f As Integer, g As Integer

   Dim i As Demand, j As Demand
   Dim m() As String, n() As Demand
   e = 1
   n = data
   Do While e <> -1

      For a = LBound(data) To UBound(data) - 1
         i = n(a)
         j = n(a + 1)
         If i.Value > j.Value Then
            n(a) = j
            n(a + 1) = i
            g = 1
         End If
      Next a
      If g = 1 Then
         e = 1
      Else
         e = -1
      End If

      g = 0
   Loop
   BubbleSortDemands = n
End Function
 
Back
Top