Monday, November 28, 2016

Bubble Sort in Collection VBA

'Sorting Collection in VBA
    For l = 1 To mycoll1.Count - 1
           'MsgBox mycoll1(l)
            For m = l + 1 To mycoll1.Count
                       
            If (IsNumeric(Mid(mycoll1(l), 1, 2))) And (IsNumeric(Mid(mycoll1(m), 1, 2))) Then
                    If CInt(Mid(mycoll1(l), 1, 2)) > CInt(Mid(mycoll1(m), 1, 2)) Then
                     'store the lesser item
                         mycoll1temp = mycoll1(m)
                     'remove the lesser item
                         mycoll1.Remove m
                      're-add the lesser item before the
                    'greater Item
                         mycoll1.Add mycoll1temp, mycoll1temp, l
                     End If
            ElseIf (IsNumeric(Mid(mycoll1(l), 1, 2))) And (IsNumeric(Mid(mycoll1(m), 1, 1))) Then
                    If CInt(Mid(mycoll1(l), 1, 2)) > CInt(Mid(mycoll1(m), 1, 1)) Then
                     'store the lesser item
                         mycoll1temp = mycoll1(m)
                     'remove the lesser item
                         mycoll1.Remove m
                      're-add the lesser item before the
                    'greater Item
                         mycoll1.Add mycoll1temp, mycoll1temp, l
                     End If
            ElseIf (IsNumeric(Mid(mycoll1(l), 1, 1))) And (IsNumeric(Mid(mycoll1(m), 1, 2))) Then
                    If CInt(Mid(mycoll1(l), 1, 1)) > CInt(Mid(mycoll1(m), 1, 2)) Then
                     'store the lesser item
                         mycoll1temp = mycoll1(m)
                     'remove the lesser item
                         mycoll1.Remove m
                      're-add the lesser item before the
                    'greater Item
                         mycoll1.Add mycoll1temp, mycoll1temp, l
                        
                     End If
            ElseIf (IsNumeric(Mid(mycoll1(l), 1, 1))) And (IsNumeric(Mid(mycoll1(m), 1, 1))) Then
                    If CInt(Mid(mycoll1(l), 1, 1)) > CInt(Mid(mycoll1(m), 1, 1)) Then
                     'store the lesser item
                         mycoll1temp = mycoll1(m)
                     'remove the lesser item
                         mycoll1.Remove m
                      're-add the lesser item before the
                    'greater Item
                         mycoll1.Add mycoll1temp, mycoll1temp, l
                        
                     End If
           
            End If
            Next
           
    Next

No comments:

Post a Comment