Search Tools Links Login

A Very Simple Sort


Visual Basic 6, or VB Classic

A very simpile example of sorting. Not the most efficeint, but easy for beginners to see whats happening.

Original Author: Paul Crowdy

Inputs

Unsorted Array, Sort order (True = Sort Ascending)

Returns

Sorted Array

Code

Private Sub Form_Load()
  Dim a(10)
  a(0) = 2
  a(1) = 5
  a(2) = 7
  a(3) = 6
  a(4) = 13
  a(5) = "b"
  a(6) = 65
  a(7) = 0
  a(8) = 4
  a(9) = "a"
  a(10) = 1000
  
  Sort a, False
  
  For i = 0 To 10
    Debug.Print a(i)
  Next i
End Sub
Sub Sort(ByRef Arr() As Variant, Optional ByVal bAsc As Boolean = True)
  Dim Done As Boolean
  Done = False
  Do While Done = False
    Done = True
    For i = 0 To UBound(Arr) - 1
      If (Arr(i) > Arr(i + 1) And bAsc) Or (Arr(i) < Arr(i + 1) And Not bAsc) Then Swap Arr(i), Arr(i + 1): Done = False
    Next i
  Loop
End Sub
Sub Swap(ByRef a As Variant, ByRef b As Variant)
  Dim tmp As Variant
  tmp = a
  a = b
  b = tmp
End Sub

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 100 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.