Here's an algotithm, called the BubbleSort Algorithm.
It can be used for sorting numbers, strings, or whatever else can be compared using VB's comparative operators.
VB Code:
Option Explicit Private Function sortInts(arrInt() As Long) As Long() Dim i As Long, n As Long, tempN As Long, allOk As Boolean For n = 0 To UBound(arrInt) allOk = True For i = 0 To UBound(arrInt) - 1 If arrInt(i) > arrInt(i + 1) Then allOk = False tempN = arrInt(i) arrInt(i) = arrInt(i + 1) arrInt(i + 1) = tempN End If Next If allOk Then Exit For Next sortInts = arrInt End Function Private Sub Form_Load() Dim x() As Long, i As Long '' redimension array to 100 values '' ReDim x(99) '' now fill with random data '' For i = 0 To 99 x(i) = CLng(Rnd * 1000) Next '' now sort the array '' x = sortInts(x) '' display the data For i = 0 To UBound(x) Debug.Print x(i) Next End Sub


Reply With Quote