Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort array question

Status
Not open for further replies.

Bastien

Programmer
May 29, 2000
1,683
CA
once I create an array...is it possible to sort those values inside the array? Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
You can do it in VB.net, but not VBA.

Take your array > dump it in a range > sort range > set array equal to it.

As far as I know, that's the best way.
 
You could always sort it yourself. This is suitable for small arrays. Certainly a merge sort is preferable for large arrays, but this method sorts up to about 5000 elements in an acceptable time on my crappy work PC.

Code:
Function BubbleSort(Data() As Integer)
    Dim Temp As Integer
    Dim i As Integer
    Dim j As Integer
    For i = 1 To UBound(Data)
        If Data(i - 1) > Data(i) Then
            j = i
            While j > 0
                If Data(j - 1) > Data(j) Then
                    Temp = Data(j)
                    Data(j) = Data(j - 1)
                    Data(j - 1) = Temp
                Else
                    j = 0
                End If
                j = j - 1
            Wend
        End If
    Next i
    BubbleSort = Data
End Function
 
Of course, I should have commented that you can change the data types of all the integers to suit your needs (double, long, etc) or even to Strings to sort those, but then the comparisons need to be replaced by the string comparison function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top