Sorting an array

There are many algorithms for sorting. Here is a sample for sorting an array.

In fact, I was going to write many samples for sorting but before starting that, I did find the very good examples on the net.

You will see many good examples on the Xtreme board. (many thanks for shareing the knowledge, Squirm!)



Option Explicit

Sub TestingFunction()
    Dim buf As Variant
    Dim vElm As Variant    'Each elemnt

    buf = Array_Sort(Array(5, 4, 6, 7, 9, 2, 1, 3, 8, 0))

    'See result
    For Each vElm In buf
        Debug.Print vElm
End Sub

Private Function Array_Sort(ByVal NotSortedArry As VariantAs Variant
'NotSortedArry must be a 1D array

'Make a sorted array
    Dim i As Long    'Counter
    Dim j As Long    'Counter
    Dim vElm As Variant    'Each elemnt

    For i = LBound(NotSortedArry) To UBound(NotSortedArry)
        For j = i + 1 To UBound(NotSortedArry)
            If NotSortedArry(i) > NotSortedArry(j) Then
                vElm = NotSortedArry(j)
                NotSortedArry(j) = NotSortedArry(i)
                NotSortedArry(i) = vElm
            End If
    Array_Sort = NotSortedArry
End Function

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo