Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Sort a Custom Class Collection in VB6

Sort a Custom Class Collection in VB6

Scheduled Pinned Locked Moved Visual Basic
algorithms
4 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KeithF
    wrote on last edited by
    #1

    Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:

    Class myClass

    private ID as String
    private Name as String
    private TK as String
    private Di as String
    private M as String

    End Class

    Using this class i add to a Collection a number of items, e.g.

    Private colClass As Collection
    Private clsTheClass As myClass

    Set colClass = New Collection

    Set clsTheClass = New myClass
    clsTheClass.Name = "A"
    clsTheClass.TKs = "100"
    clsTheClass.Di = "5"
    clsTheClass.ID = "1"
    clsTheClass.M = "9"
    
    colClass.Add clsTheClass
    

    What i end up with it is a collection that looks like so after adding all the items:

    ID |Name |TK |Di |M
    1 A 100 5 9
    2 B 100 3 9
    3 C 10 5 9
    4 D 10 7 9
    5 E 10 1 9
    6 F 400 9 5
    7 F 400 4 5

    I need to sort the collection On TK, and then DI. That should make the collection look like this:

    ID |Name |TK |Di |M
    6 F 400 9 5
    7 F 400 4 5
    1 A 100 5 9
    2 B 100 3 9
    4 D 10 7 9
    3 C 10 5 9
    5 E 10 1 9

    I am currently using this method to sort on TK, but i cannot get it to work how i need the output:

    Public Sub SortCollection(ColVar As Collection)
    Dim oCol As Collection
    Dim i As Integer
    Dim i2 As Integer
    Dim iBefore As Integer

    If Not (ColVar Is Nothing) Then
    
        If ColVar.Count > 0 Then
        
            Set oCol = New Collection
            
            For i = 1 To ColVar.Count
            
                If oCol.Count = 0 Then
                    oCol.Add ColVar(i)
                Else
                    iBefore = 0
                    
                    For i2 = oCol.Count To 1 Step -1
                    
                        If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then
                            iBefore = i2
                        Else
                            Exit For
                        End If
                        
                    Next
                    
                    If iBefore = 0 Then
                        oCol.Add ColVar(i)
                    Else
                        oCol.Add ColVar(i), , iBefore
                    End If
    
    _ P R 3 Replies Last reply
    0
    • K KeithF

      Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:

      Class myClass

      private ID as String
      private Name as String
      private TK as String
      private Di as String
      private M as String

      End Class

      Using this class i add to a Collection a number of items, e.g.

      Private colClass As Collection
      Private clsTheClass As myClass

      Set colClass = New Collection

      Set clsTheClass = New myClass
      clsTheClass.Name = "A"
      clsTheClass.TKs = "100"
      clsTheClass.Di = "5"
      clsTheClass.ID = "1"
      clsTheClass.M = "9"
      
      colClass.Add clsTheClass
      

      What i end up with it is a collection that looks like so after adding all the items:

      ID |Name |TK |Di |M
      1 A 100 5 9
      2 B 100 3 9
      3 C 10 5 9
      4 D 10 7 9
      5 E 10 1 9
      6 F 400 9 5
      7 F 400 4 5

      I need to sort the collection On TK, and then DI. That should make the collection look like this:

      ID |Name |TK |Di |M
      6 F 400 9 5
      7 F 400 4 5
      1 A 100 5 9
      2 B 100 3 9
      4 D 10 7 9
      3 C 10 5 9
      5 E 10 1 9

      I am currently using this method to sort on TK, but i cannot get it to work how i need the output:

      Public Sub SortCollection(ColVar As Collection)
      Dim oCol As Collection
      Dim i As Integer
      Dim i2 As Integer
      Dim iBefore As Integer

      If Not (ColVar Is Nothing) Then
      
          If ColVar.Count > 0 Then
          
              Set oCol = New Collection
              
              For i = 1 To ColVar.Count
              
                  If oCol.Count = 0 Then
                      oCol.Add ColVar(i)
                  Else
                      iBefore = 0
                      
                      For i2 = oCol.Count To 1 Step -1
                      
                          If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then
                              iBefore = i2
                          Else
                              Exit For
                          End If
                          
                      Next
                      
                      If iBefore = 0 Then
                          oCol.Add ColVar(i)
                      Else
                          oCol.Add ColVar(i), , iBefore
                      End If
      
      _ Offline
      _ Offline
      _Marshall
      wrote on last edited by
      #2

      Hi Keith Is it known how many elements are added to the collection or is that defined at runtime?

      1 Reply Last reply
      0
      • K KeithF

        Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:

        Class myClass

        private ID as String
        private Name as String
        private TK as String
        private Di as String
        private M as String

        End Class

        Using this class i add to a Collection a number of items, e.g.

        Private colClass As Collection
        Private clsTheClass As myClass

        Set colClass = New Collection

        Set clsTheClass = New myClass
        clsTheClass.Name = "A"
        clsTheClass.TKs = "100"
        clsTheClass.Di = "5"
        clsTheClass.ID = "1"
        clsTheClass.M = "9"
        
        colClass.Add clsTheClass
        

        What i end up with it is a collection that looks like so after adding all the items:

        ID |Name |TK |Di |M
        1 A 100 5 9
        2 B 100 3 9
        3 C 10 5 9
        4 D 10 7 9
        5 E 10 1 9
        6 F 400 9 5
        7 F 400 4 5

        I need to sort the collection On TK, and then DI. That should make the collection look like this:

        ID |Name |TK |Di |M
        6 F 400 9 5
        7 F 400 4 5
        1 A 100 5 9
        2 B 100 3 9
        4 D 10 7 9
        3 C 10 5 9
        5 E 10 1 9

        I am currently using this method to sort on TK, but i cannot get it to work how i need the output:

        Public Sub SortCollection(ColVar As Collection)
        Dim oCol As Collection
        Dim i As Integer
        Dim i2 As Integer
        Dim iBefore As Integer

        If Not (ColVar Is Nothing) Then
        
            If ColVar.Count > 0 Then
            
                Set oCol = New Collection
                
                For i = 1 To ColVar.Count
                
                    If oCol.Count = 0 Then
                        oCol.Add ColVar(i)
                    Else
                        iBefore = 0
                        
                        For i2 = oCol.Count To 1 Step -1
                        
                            If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then
                                iBefore = i2
                            Else
                                Exit For
                            End If
                            
                        Next
                        
                        If iBefore = 0 Then
                            oCol.Add ColVar(i)
                        Else
                            oCol.Add ColVar(i), , iBefore
                        End If
        
        P Offline
        P Offline
        PrissySC
        wrote on last edited by
        #3

        Whenever I sort, I find the largest, or the least first and set the value to "x". From there I work up, or down assigning to a temp object, collection or array, and replacing "x" as the largest each time. For example, x = 0 for each item in collection if item > x then x = item end if next This gets the largest to begin. tempCollection.add(x) while tempcollection.count isnot collection.count for each item in collection set x if the item is less than the last in tempcollection and is not less than x next tempcollection.add(x) loop That is a level one sort, but you should be able to expand to code easily enough. Sorry that I lack any time, or I would have done it for you!

        1 Reply Last reply
        0
        • K KeithF

          Hi Folks, I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:

          Class myClass

          private ID as String
          private Name as String
          private TK as String
          private Di as String
          private M as String

          End Class

          Using this class i add to a Collection a number of items, e.g.

          Private colClass As Collection
          Private clsTheClass As myClass

          Set colClass = New Collection

          Set clsTheClass = New myClass
          clsTheClass.Name = "A"
          clsTheClass.TKs = "100"
          clsTheClass.Di = "5"
          clsTheClass.ID = "1"
          clsTheClass.M = "9"
          
          colClass.Add clsTheClass
          

          What i end up with it is a collection that looks like so after adding all the items:

          ID |Name |TK |Di |M
          1 A 100 5 9
          2 B 100 3 9
          3 C 10 5 9
          4 D 10 7 9
          5 E 10 1 9
          6 F 400 9 5
          7 F 400 4 5

          I need to sort the collection On TK, and then DI. That should make the collection look like this:

          ID |Name |TK |Di |M
          6 F 400 9 5
          7 F 400 4 5
          1 A 100 5 9
          2 B 100 3 9
          4 D 10 7 9
          3 C 10 5 9
          5 E 10 1 9

          I am currently using this method to sort on TK, but i cannot get it to work how i need the output:

          Public Sub SortCollection(ColVar As Collection)
          Dim oCol As Collection
          Dim i As Integer
          Dim i2 As Integer
          Dim iBefore As Integer

          If Not (ColVar Is Nothing) Then
          
              If ColVar.Count > 0 Then
              
                  Set oCol = New Collection
                  
                  For i = 1 To ColVar.Count
                  
                      If oCol.Count = 0 Then
                          oCol.Add ColVar(i)
                      Else
                          iBefore = 0
                          
                          For i2 = oCol.Count To 1 Step -1
                          
                              If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then
                                  iBefore = i2
                              Else
                                  Exit For
                              End If
                              
                          Next
                          
                          If iBefore = 0 Then
                              oCol.Add ColVar(i)
                          Else
                              oCol.Add ColVar(i), , iBefore
                          End If
          
          R Offline
          R Offline
          Roy Heil
          wrote on last edited by
          #4

          You could implement the IComparable interface in your class, and then define the CompareTo function. Put your comparison logic in this CompareTo function. Then all you would need to do is call the Sort method on your collection. MyNewCol.Sort()

          Roy.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups